I've got an immutable map of maps.
let mapOfMaps = Immutable.fromJS({
'abc': {
id: 1
type: 'request'
},
'def': {
id: 2
type: 'response'
},
'ghi': {
type: cancel'
},
'jkl': {
type: 'edit'
}
});
How can I
I don't have the option of switching to a List at this stage.
I don't know how to loop through the keys.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With