jaroApp
jaroApp

Reputation: 899

ImmutableJS Map or OrderedMap change position

I have Map and OrderedMap objects. I need to key different then 0,1,2 this is why I don't use List. But I need to add arrows to move up/down elements in this list. Also it would be good to set "insert after" not only as last element.

I know that it sounds like I need to List, but too many function based on keys different than 0,1,2 to change this objects.

Any hints how can I move elements inside this kind of elements?

Thanks in advance for any hints.

Upvotes: 0

Views: 1366

Answers (1)

zeachco
zeachco

Reputation: 780

So let say you have a map like this

fromJS({
  saturn: 'planet',
  earth: 'planet,
  sun: 'star'
})

You can use

immutable.get('items')
  .keySeq()
  .indexOf("earth")

// returns 1

The result will be the index if the Map were a List or Array. -1 would be returned if not found.

On the other hand, if you need to find the map key with the index, you can do this:

immutable.get('items')
  .keySeq()
  .findIndex(k => k === 1)

// returns "earth"

Upvotes: 1

Related Questions