Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjs: map with index

Tags:

angular

rxjs

I want to know the index of the current object when map is used. 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?

like image 973
user1670773 Avatar asked Oct 17 '25 11:10

user1670773


1 Answers

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
like image 156
Andrei Avatar answered Oct 19 '25 02:10

Andrei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!