官术网_书友最值得收藏!

Chaining collection mutation methods

You've seen how you can chain collection insertion methods together and that there are two types of collection mutation method. Since each of these methods is a persistent change, they return a new collection, which means that you can chain together complex mutation behavior:

const myList = List.of(1, 2, 3);
const myMap = Map.of('one', 1, 'two', 2);

const myChangedList = myList
.push(4)
.set(3, 5)
.update(3, n => n + 5);

const myChangedMap = myMap
.set('three', 3)
.set('three', 5)
.update('three', t => t + 5);

console.log('myList', myList.toJS());
// -> myList [ 1, 2, 3 ]
console.log('myMap', myMap.toJS());
// -> myMap { one: 1, two: 2 }
console.log('myChangedList', myChangedList.toJS());
// -> myChangedList [ 1, 2, 3, 10 ]
console.log('myChangedMap', myChangedMap.toJS());
// -> myChangedMap { one: 1, two: 2, three: 10 }

Here, we're mutating a list and a map by chaining together insertion methods and mutation methods. A key design trait of the Immutable.js API is that the list methods and the map methods are the same. The obvious difference is push() versus set() for inserting items into lists and maps.

主站蜘蛛池模板: 东兴市| 壶关县| 潜山县| 梅河口市| 蓬莱市| 京山县| 景东| 新营市| 宕昌县| 弥勒县| 芜湖县| 交城县| 布尔津县| 永平县| 芦溪县| 乡宁县| 比如县| 农安县| 青河县| 公安县| 乌审旗| 清丰县| 北宁市| 富民县| 南平市| 周宁县| 宁南县| 无锡市| 嘉兴市| 道孚县| 天镇县| 自治县| 扎兰屯市| 安庆市| 泰安市| 子长县| 潼南县| 贵南县| 天镇县| 图木舒克市| 沁水县|