When filtering maps, it's helpful to be able to filter by keys. With lists, you're less likely to filter values by their indexes. Keys, on the other hand, usually have meaning within the context of your application.
Having maps with simple strings as keys is common. You can use them in your filter()
callback functions because keys are passed as the second argument:
const myMap = Map.of( 'one', 1, 'two', 2, 'three', 3 ); const myFilteredMap = myMap.filter( (v, k) => k.includes('o') ); console.log('myMap', myMap.toJS()); // -> myMap { one: 1, two: 2, three: 3 } console.log('myFilteredMap', myFilteredMap.toJS()); // -> myFilteredMap { one: 1, two: 2 }
As you can see, myFilteredMap
only includes values whose keys include the letter o
.