angular - rxjs:带索引的地图
问题描述
我想知道使用地图时当前对象的索引。例如:
x = [3,2,6]
from(x).pipe(
map(index, val => (val, index))
).subscribe((val, index) => console.log(val, index))
预期产出
3, 0
2, 1
6, 2
基本上,我想知道数组中元素的索引。我怎样才能做到这一点?
解决方案
它与您尝试过的非常接近
from(x).pipe(
map((val, index) => [val, index]) // here we transform event to array (call it tuple if you like)
).subscribe(([val, index]) => console.log(val, index)) // here in params we destructure tuple to values again
推荐阅读
- c# - 在 Polly CircuitBreaker 中设置 durationOfBreak
- android - 无法解决:Firebase auth 15.0.0
- c++ - 如何将 QListWidget 中的当前选择设置为无?
- html - 滚动时如何将标题更改为可移动标题?
- excel - Excel Range 不那么笨拙
- r - 在 R 中从外部 jpg 创建 GIF 或视频
- c++ - 在 C++ 模板参数中,是否可以指定该类型的类型和出现次数?
- java - 从泛型方法中返回 this
- html - 带有绝对定位的儿童不工作的旋转木马
- php - 如果我想检查 PDOstmt->execute() 返回的内容,我是否在 if 语句中再次调用它?