1
内如果我有一个不变的列表,如:如何更新地图列表
List([
Map({ something: 1 }),
Map({ something: 2 }),
])
我怎会设置的东西= 5,其中一些= 1?
内如果我有一个不变的列表,如:如何更新地图列表
List([
Map({ something: 1 }),
Map({ something: 2 }),
])
我怎会设置的东西= 5,其中一些= 1?
console.clear();
const list = Immutable.List([
Immutable.Map({ something: 1 }),
Immutable.Map({ something: 2 }),
Immutable.Map({ something: 4 }),
Immutable.Map({ something: 1 })
])
const newList = list.map(item =>
item.get("something") === 1 ? item.set("something", 5) : item
);
console.log('Old',JSON.stringify(list.toArray(),null, ""));
console.log('New',JSON.stringify(newList.toArray(),null, ""));
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.2/immutable.min.js"></script>
@ QOP的回答会是不错的,如果你正在寻找一个特定的键。
如果要更新每个地图中的多个值,可以在list.map调用中使用.mapEntries()。
things
.map(thing => {
return thing.mapEntries(([ k, v ]) => {
const val = (v === 5) ? 1 : v;
return [ k, val ]
})
});