How to loop through an Immutable Map of Immutable Maps?
With keySeq()
/valueSeq()
method you get sequence of keys/values. Then you can iterate it for example with forEach()
:
let mapOfMaps = Immutable.fromJS({
abc: {
id: 1,
type: 'request'
},
def: {
id: 2,
type: 'response'
},
ghi: {
type: 'cancel'
},
jkl: {
type: 'edit'
}
});
// iterate keys
mapOfMaps.keySeq().forEach(k => console.log(k));
// iterate values
mapOfMaps.valueSeq().forEach(v => console.log(v));
Furthermore you can iterate both in one loop with entrySeq()
:
mapOfMaps.entrySeq().forEach(e => console.log(`key: ${e[0]}, value: ${e[1]}`));
If we need key:value together, we can use for
loop also. for
loop provides flexibility to put a break;
for a desired condition match.
//Iterating over key:value pair in Immutable JS map object.
for(let [key, value] of mapOfMaps) {
console.log(key, value)
}