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

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.

主站蜘蛛池模板: 麻栗坡县| 浦江县| 洞口县| 大荔县| 彭山县| 丹江口市| 谷城县| 泉州市| 高邮市| 天祝| 延安市| 柳州市| 南康市| 河北区| 阳东县| 恩平市| 景东| 通山县| 嘉义市| 雅江县| 三明市| 天峻县| 莱州市| 西藏| 淄博市| 雷山县| 新巴尔虎右旗| 福贡县| 太湖县| 凤凰县| 白沙| 南汇区| 庐江县| 自贡市| 射阳县| 浙江省| 新宁县| 黄冈市| 桓台县| 青浦区| 肥乡县|