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

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.

主站蜘蛛池模板: 遂川县| 家居| 贵溪市| 鹤岗市| 含山县| 平湖市| 桑日县| 手机| 手游| 宣武区| 台东县| 昔阳县| 交口县| 河间市| 罗城| 柏乡县| 格尔木市| 玉门市| 四会市| 东至县| 新田县| 乐昌市| 上虞市| 阳曲县| 姚安县| 安远县| 克拉玛依市| 永登县| 无棣县| 元朗区| 敦化市| 墨脱县| 平遥县| 菏泽市| 乌什县| 甘泉县| 苏尼特左旗| 卓尼县| 偏关县| 五峰| 石嘴山市|