在ImmutableJS中,如何将新数组推入Map? [英] In ImmutableJS, how to push a new array into a Map?
本文介绍了在ImmutableJS中,如何将新数组推入Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用ImmutableJS实现以下目标:
How can I achieve the following using ImmutableJS:
myMap.get(key).push(newData);
推荐答案
您可以执行以下操作:(请参阅此 JSBin )
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"]
// }
// }
由于myMap
是不可变的,因此每当您尝试设置/更新/删除其中的某些数据时,它将返回对新数据的引用.因此,您必须将其设置为变量才能访问它(在本例中为myNewMap
).
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
).
这篇关于在ImmutableJS中,如何将新数组推入Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文