Flattening nested maps

Another common structure is deeply-nested maps. Let's imagine that you want to filter keys, including nested keys, by flattening the nested map structure:

const myMap = Map.of(
'first', 1,
'second', Map.of(
'third', 3,
'fourth', 4,
'fifth', Map.of(
'sixth', Map.of(
'seventh', 7
)
)
)
);

console.log('myMap', myMap.toJS());
// -> { first: 1,
// -> second: {
// -> third: 3,
// -> fourth: 4,
// -> fifth: {
// -> sixth: { seventh: 7 } } } }

myMap
.toSeq()
.flatten()
.filter((v, k) => k.startsWith('f'))
.forEach((v, k) => console.log(k, v));
// -> first 1
// -> fourth 4

Here, we are able to find all nested values whose key started with 'f'. One of the keys—'fifth'—starts with 'f', but it doesn't appear in the results. When you flatten maps, the flattened sequence doesn't include keys whose values are collections.

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

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