Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Read Values Within Arrays In Immutable.js

Im a bit confused about this and cant seem to work this out.

Say I have this:

const AnObj = Immutable.Map({
 a : "a",
 b : Immutable.List.of(
  a,
  b,
  c,
  Immutable.Map({
   a : "a"
  })
 )
});

With Immutable Maps, we use strings within get() to find the corresponding properties. How do we read array values?

like image 228
Kayote Avatar asked Feb 07 '23 02:02

Kayote


1 Answers

Disclaimer - This applies to all Immutable types, not just List.

Several ways -

  1. The get method - AnObj.get('b').get(3).get('a') (Thanks @stas). This is useful when the structure is not very deep. As you see, the syntax is very verbose.

  2. The succinct getIn - AnObj.getIn(['b', 3, 'a']) I love this because this pattern allows having a generic getter and I can toss the key-path around to the various components.

  3. The veritable valueSeq/entrySeq, when you want all the values and don't care for indices - AnObj.get('b').valueSeq() This is useful when the list is huge and you want to delay the iteration until its absolutely needed. This is the most performant of them all.

like image 101
hazardous Avatar answered Feb 11 '23 00:02

hazardous