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

Chaining collection removal methods

If you have more than one value to remove from a collection, you can chain together removal method calls, as follows:

const myList = List.of(1, 2, 3, 4);
const myMap = Map.of(
'one', 1, 'two', 2,
'three', 3, 'four', 4,
'five', 5, 'six', 6
);

const myChangedList = myList
.remove(1)
.remove(1);

const myChangedMap = myMap
.remove('six')
.removeAll(['five', 'four', 'three']);

console.log('myList', myList.toJS());
// -> myList [ 1, 2, 3, 4 ]
console.log('myMap', myMap.toJS());
// -> myMap { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }
console.log('myChangedList', myChangedList.toJS());
// -> myChangedList [ 1, 4 ]
console.log('myChangedMap', myChangedMap.toJS());
// -> myChangedMap { one: 1, two: 2 }

There are two identical removal calls to the list—remove(1). What's up with that? Chaining removal calls to remove list items can be tricky because removing one value causes every value to the right of it to change its index. At first, the index 1 points to the value 2. Once it's removed, the index 1 points to the value 3.

With maps, you have a couple of options for removing more than one key-value pair. You can chain together calls to remove(), or you can pass multiple keys to the removeAll() method.

主站蜘蛛池模板: 旬邑县| 嘉善县| 寻乌县| 雷州市| 舞阳县| 黑河市| 深泽县| 台江县| 石景山区| 若羌县| 饶河县| 旺苍县| 当涂县| 龙陵县| 抚松县| 铜鼓县| 蓬溪县| 潞西市| 鄂托克前旗| 南华县| 景谷| 汝州市| 蒙山县| 永胜县| 昌江| 吴江市| 澄江县| 丽江市| 岳普湖县| 延庆县| 嘉义市| 忻州市| 建昌县| 定远县| 莆田市| 九台市| 贵南县| 定结县| 盐边县| 大余县| 大宁县|