Mapping lists of maps
Lists of maps are common in Immutable.js applications. Also common is the need to map these lists to lists of simple values. This means taking a simple value from the map, or using the map to compute a simple value.
Plucking values
Plucking a value from a map is another way of saying look up a value based on a key. It's common to say "pluck" when mapping a collection of maps to a particular key. Imagine plucking blades of grass from a lawn that fit a particular profile. Let's look at an example:
const myList = List.of( Map.of('first', 1, 'second', 2), Map.of('first', 3, 'second', 4), Map.of('first', 5, 'second', 6) ); const myMappedList = myList .map(v => v.get('second')); console.log('myList', myList.toJS()); // -> myList [ { first: 1, second: 2 }, // -> { first: 3, second: 4 }, // -> { first: 5, second: 6 } ] console.log('myMappedList', myMappedList.toJS()); // -> myMappedList [ 2, 4, 6 ]
The myMappedList
list now has the values...