Filtering fancy keys

Unlike plain JavaScript objects, Immutable.js maps can use anything as a key. A common use of this capability is to associate data with a map that you don't necessarily want to add to the map as a value. You can then filter the map based on the maps used as keys:

const myFancyMap = Map.of(
Map.of('name', 'one'), 1,
Map.of('name', 'two'), 2,
Map.of('name', 'three'), 3
);
const myFilteredMap = myFancyMap.filter(
(v, k) => k.get('name').startsWith('t')
);

console.log('myFancyMap', myFancyMap.toJS());
// -> myFancyMap
// -> { 'Map { "name": "one" }': 1,
// -> 'Map { "name": "two" }': 2,
// -> 'Map { "name": "three" }': 3 }
console.log('myFilteredMap', myFilteredMap.toJS());
// -> myFilteredMap
// -> { 'Map { "name": "two" }': 2,
// -> 'Map { "name": "three" }': 3 }

There's really no limit to what you can filter with maps that have complex keys, such as other maps. In the preceding example, we got the numbers associated with keys that have a name property beginning with the letter t.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.0.53