As the title states, I want to get the index of a particular item. Is there a way to do this?
const key = 1
const map = new Immutable.OrderedMap([5, 'a'], [3, 'b'], [1, 'c'])
So, in this case, the index of key
would be 2
.
You can get the key sequence from the map:
let index = map.keySeq().findIndex(k => k === key);
See the docs for more info.
Alternatively, you could explicitly iterate over the keys and compare them:
function findIndexOfKey(map, key) {
let index = -1;
for (let k of map.keys()) {
index += 1;
if (k === key) {
break
}
}
return index;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With