第六章 集合引用类型
Array
创建数组
通过new 方法创建
Array.from将可迭代结构或者存在length属性和可索引元素的结构转为数组
1 2 3 4
| const a = [1, 2, 3, 4] Array.from(a) Array.from(a, x => x * 2) Array.from(a, x => x * this.exponent, {exponent: 2})
|
Array.of将一组参数组合为数组
判断是否是数组
数组迭代器
1 2 3 4
| const a = Array.of("foo", "bar", "baz") Array.from(a.keys()) Array.from(a.values()) Array.from(a.entries())
|
操作方法
使用Symbol.isConcatSpreadable=false,来使concat不会打平数组
1 2 3 4 5 6 7 8 9 10
| const alpha = ['a', 'b', 'c']; const numeric = [1, 2, 3]; let alphaNumeric = alpha.concat(numeric); console.log(alphaNumeric);
numeric[Symbol.isConcatSpreadable] = false; alphaNumeric = alpha.concat(numeric); console.log(alphaNumeric);
|
slice方法,创建一个包含原数组元素的新数组
1 2 3
| slice(start, end) const nums = [1, 2, 3] nums.slice(-2, -1) => nums.slice(1, 2) => [ 2 ]
|
splice方法,对原数组进行添加/删除元素
1 2 3 4
| splice(start, deleteCount, insertElement) const nums = [1, 2, 3, 4, 5] nums.splice(0, 2) nums.splice(0, 2, 6, 7)
|
迭代方法
every(): 对数组每一项都运行传入的函数,如果每一行函数返回true,则这个方法返回true
1 2
| const nums = [1, 2, 3, 4, 5, 6, 7] const res = nums.every((item, index, array) => item > 2)
|
filter(): 对数组每一项运行传入的函数,函数返回为true的项会组成新的数组
1 2
| const nums = [1, 2, 3, 4, 5, 6, 7] const res = nums.filter((item, index, array) => item > 2)
|
map(): 对数组每一项运行传入的函数,返回由每次函数调用结果组成的数组
1 2
| const nums = [1, 2, 3, 4, 5, 6, 7] const res = nums.map((item, index, array) => item * 2)
|
some(): 对数组每一项运行传入的函数,如果有一项函数返回true,则这个方法返回true
1 2
| const nums = [1, 2, 3, 4, 5, 6, 7] const res = nums.some((item, index, array) => item > 2)
|
Map
基础方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| const map = new Map([ ["key1", "value1"], ["key2", "value2"], ]) map.has("key1") map.get("key1") map.set("key3", "value3") map.size() map.delete("key1") map.clear()
for (let pair of map.entries()) {} for (let key of map.keys()) {} for (let value of map.values()) {} [...map]
|
Map和Object区别
- 内存占用,固定大小内存,Map大约可以比Object多存储50%的键值对
- 插入性能,消耗大致相当,不过Map在浏览器中会稍微快一点
- 查找速度,当把Object当作数组使用(连续整数作为属性),浏览器会进行优化,查找速度会快一点
- 删除性能,使用Map
WeakMap
弱映射中的键只能是object或者继承object的类型,弱映射不会阻止垃圾回收程序
1 2 3 4 5 6 7
| const wm = new WeakMap() const key1 = {id: 1} const key2 = {id: 2} wm.has(key1) wm.get(key1) wm.set(key1, "test") wm.delete(key1)
|