简体   繁体   English

rxjs:带索引的地图

[英]rxjs: map with index

I want to know the index of the current object when map is used.我想知道使用 map 时当前对象的索引。 For example:例如:

x = [3,2,6]
from(x).pipe(
 map(index, val => (val, index))
).subscribe((val, index) => console.log(val, index))

Expected output预期输出

3, 0
2, 1
6, 2

Basically, I want to know the index of the element in the array.基本上,我想知道数组中元素的索引。 How can I do this?我怎样才能做到这一点?

it is very close to what you've tried它与您尝试过的非常接近

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM