Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ImmutableJS, how to push a new array into a Map?

Tags:

immutable.js

How can I achieve the following using ImmutableJS:

myMap.get(key).push(newData);

like image 365
Chopper Lee Avatar asked Jul 27 '15 09:07

Chopper Lee


1 Answers

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).

like image 63
Brian Park Avatar answered Oct 07 '22 13:10

Brian Park