Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The index of an item in OrderedMap

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.

like image 809
Azad Salahli Avatar asked Sep 10 '15 20:09

Azad Salahli


1 Answers

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;
}
like image 99
Felix Kling Avatar answered Oct 06 '22 00:10

Felix Kling