回顾ImmutableJS Map操作

初始值可以用set, setIn。
update更新

const aMap = Immutable.Map({ apples: 10 })
const bMap = aMap.update("apples", v => v + 1)
bMap.get("apples")
=> 11

const cMap = aMap.update("oranges", () => 6)
bMap.get("oranges")
=> 6

等价于updateIn

const aMap = Immutable.Map({ apples: 10 })
const bMap = aMap.updateIn(["apples"], v => v + 1)
bMap.get("apples")
=> 11

const cMap = aMap.updateIn(["oranges"], () => 6)
bMap.get("oranges")
=> 6

嵌套应该用updateIn getIn

const map = Map({ a:Map({ b:Map({ c: 10 }) }) })
//获取a
map.get("a")
//等价于
map.getIn(["a"])

//获取b
map.getIn(["a", "b"])
//获取c
map.getIn(["a", "b", "c"])

//更新c
const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
// Map { "a": Map { "b": Map { "c": 20 } } }

//更新x,示例初始参数100
const newMap = map.updateIn(['a', 'b', 'x'], (val = 100) => val)
// Map { "a": Map { "b": Map { "c": 10, "x": 100 } } }

参考官方文档
https://facebook.github.io/immutable-js/docs/#/Map/updateIn

猜你喜欢

转载自blog.csdn.net/wlchn/article/details/78206331