回顾ImmutableJS Map操作

来源:互联网 发布:知秋取名意思是什么 编辑:程序博客网 时间:2024/05/13 21:00

初始值可以用set, setIn。
update更新(推荐)

const aMap = Immutable.Map({ apples: 10 })const bMap = aMap.update("apples", v => v + 1)bMap.get("apples")=> 11const 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")=> 11const cMap = aMap.update(["oranges"], () => 6)bMap.get("oranges")=> 6

嵌套应该用updateIn getIn

const map = Map({ a:Map({ b:Map({ c: 10 }) }) })//获取amap.get("a")//等价于map.getIn(["a"])//获取bmap.getIn(["a", "b"])//获取cmap.getIn(["a", "b", "c"])//更新cconst newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)// Map { "a": Map { "b": Map { "c": 20 } } }//更新x,示例初始参数100const 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