How can I achieve the following using ImmutableJS:
myMap.get(key).push(newData);
You can do as follows: (see this JSBin)
const myMap = Immutable.fromJS({
nested: {
someKey: ['hello', 'world'],
},
});
const myNewMap = myMap.updateIn(['nested', 'someKey'], arr => arr.push('bye'));
console.log(myNewMap.toJS());
// {
// nested: {
// someKey: ["hello", "world", "bye"]
// }
// }
Since myMap
is immutable, whenever you try to set/update/delete some data within it, it will return a reference to the new data. So, you would have to set it to a variable in order to access it (in this case, myNewMap
).
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