- Mastering Immutable.js
- Adam Boduch
- 207字
- 2021-07-08 10:30:12
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.
推薦閱讀
- 密碼學原理與Java實現
- Monkey Game Development:Beginner's Guide
- Getting Started with ResearchKit
- Django開發從入門到實踐
- Learning SAP Analytics Cloud
- PHP 7+MySQL 8動態網站開發從入門到精通(視頻教學版)
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- Statistical Application Development with R and Python(Second Edition)
- Beginning C++ Game Programming
- Learning Splunk Web Framework
- Hadoop大數據分析技術
- 大規模語言模型開發基礎與實踐
- Java EE項目應用開發
- 計算機系統解密:從理解計算機到編寫高效代碼
- Mastering ASP.NET Web API