|
11 | 11 | /** |
12 | 12 | * 常用方法 |
13 | 13 | */ |
14 | | -// Array.of() 将参数中所有值作为元素形成数组 |
15 | | -// Array.from() 将类数组对象或可迭代对象转化为数组 |
16 | | -// find() 查找数组中第一个符合条件的元素 |
17 | | -// findIndex() 查找数组中第一个符合条件的元素索引 |
18 | | -// fill() 将一定范围索引的数组元素内容填充为单个指定的值 |
| 14 | +// * Array.of() 【创建】将参数中所有值作为元素形成数组 |
| 15 | +// * Array.from() 【创建】将类数组对象或可迭代对象转化为数组 |
| 16 | +// * fill() 【填充】将一定范围索引的数组元素内容填充为单个指定的值 |
19 | 17 | // copyWithin() 将一定范围索引的数组元素修改为此数组另一指定范围索引的元素 |
| 18 | + |
| 19 | +// * find() 【查找】数组中(第一个)符合条件的元素 |
| 20 | +// * findIndex() 【查找】数组中(第一个)符合条件的元素索引 |
20 | 21 | // entries() / next 遍历键值对 |
21 | 22 | // keys() 遍历键名 |
22 | 23 | // values() 遍历键值 |
|
35 | 36 | console.log(Array.of()); // [] |
36 | 37 |
|
37 | 38 | // 【 Array.from() 】 将类数组对象或可迭代对象转化为数组 |
38 | | -// Array.from(arrayLike[, mapFn[, thisArg]]) |
| 39 | +// Array.prototype.from(arrayLike[, mapFn[, thisArg]]) |
39 | 40 | // arrayLike 必选,类数组对象,即一个对象必须含有length属性,且元素属性名必须是数值或者可转换为数值的字符 |
40 | 41 | // mapFn 可选,map 函数,用于对每个元素进行处理,放入数组的是处理后的元素 |
41 | 42 | // thisArg 可选,用于指定 map 函数执行时的 this 对象 |
| 43 | +// 伪数组特征,值按索引存储,包含length属性 |
| 44 | +function test () { |
| 45 | + let args = Array.from(arguments) |
| 46 | + let imgs = Array.from(document.querySelectorAll('img')) |
| 47 | + let array1 = Array.from({ length: 5 }) |
| 48 | +} |
42 | 49 | console.log(Array.from([1, , 3])); // [1, undefined, 3] |
43 | 50 | console.log(Array.from([1, 2, 3], (n) => n * 2)); // [2, 4, 6] |
44 | 51 | let arrayLike = { |
|
83 | 90 | let str = 'abc'; |
84 | 91 | console.log(Array.from(str)); // ["a", "b", "c"] |
85 | 92 |
|
86 | | -/** |
87 | | - * es6 数组查找 |
88 | | - */ |
89 | | -// 【 find() 】 查找数组中符合条件的元素,若有多个则返回第一个元素 |
90 | | -let arr = Array.of(1, 2, 3, 4); |
91 | | -console.log(arr.find(item => item > 2)); // 3 |
92 | | -console.log([, 1].find(n => true)); // undefined 数组空位处理为 undefined |
93 | | -// 【 findIndex() 】 查找数组中符合条件的元素索引,若有多个则返回第一个元素索引 |
94 | | -// 参数2(可选):指定回调函数中的 this 值 |
95 | | -let arr = Array.of(1, 2, 1, 3); |
96 | | -console.log(arr.findIndex(item => item = 1)); // 0 |
97 | | -console.log([, 1].findIndex(n => true)); // 0 数组空位处理为 undefined |
98 | | - |
99 | 93 | /** |
100 | 94 | * es6 数组填充 |
101 | 95 | */ |
102 | 96 | // 【 fill() 】 将一定范围索引的数组元素内容填充为单个指定的值 |
103 | 97 | // 参数1:用来填充的值 |
104 | | -// 参数2:被填充的起始索引 |
| 98 | +// 参数2(可选):被填充的起始索引,默认0 |
105 | 99 | // 参数3(可选):被填充的结束索引,默认为数组末尾 |
| 100 | +Array(5).fill(1) // [1,1,1,1,1] |
106 | 101 | let arr = Array.of(1, 2, 3, 4); |
107 | 102 | console.log(arr.fill(0,1,2)); // [1, 0, 3, 4] |
108 | 103 | // 【 copyWithin() 】 将一定范围索引的数组元素修改为此数组另一指定范围索引的元素 |
|
113 | 108 | console.log([1, 2, 3, 4].copyWithin(-2, 0)); // [1, 2, 1, 2] |
114 | 109 | console.log([1, 2, ,4].copyWithin(0, 2, 4)); // [, 4, , 4] |
115 | 110 |
|
| 111 | +/** |
| 112 | + * es6 数组查找 |
| 113 | + */ |
| 114 | +// 【 find() 】 查找数组中符合条件的元素,若有多个则返回第一个元素 |
| 115 | +let arr = Array.of(1, 2, 3, 4); |
| 116 | +console.log(arr.find(item => item > 2)); // 3 |
| 117 | +console.log([, 1].find(n => true)); // undefined 数组空位处理为 undefined |
| 118 | +// 【 findIndex() 】 查找数组中符合条件的元素索引,若有多个则返回第一个元素索引 |
| 119 | +// 参数2(可选):指定回调函数中的 this 值 |
| 120 | +let arr = Array.of(1, 2, 1, 3); |
| 121 | +console.log(arr.findIndex(item => item = 1)); // 0 |
| 122 | +console.log([, 1].findIndex(n => true)); // 0 数组空位处理为 undefined |
| 123 | + |
116 | 124 | /** |
117 | 125 | * es6 数组遍历 |
118 | 126 | */ |
|
0 commit comments