1.可迭代的空数组###
我们知道,Javacript的所创建的数组如果未被初始化值的话,它是一个稀疏数组(sparse array),这是什么意思的?我们先看下面的代码
1 | const arr = new Array(4); //[undefined, undefined, undefined, undefined] |
假设我们要去迭代一个数组,我们会发现,未被初始化值的数组,index实际上访问不到的:
1 | arr.map((elem, index) => index); // 输出:[undefined, undefined, undefined, undefined] |
我们分别来看Array.prototype.forEach和Array.prototype.map的描述,可以留意到:
1 | map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values, including undefined. It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value). |
1 | forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays). |
对于forEach
与map
这类方法,如果数组有未被初始化值的index, 默认是不回被遍历的。
那么, 如何创建一个所有值默认可被遍历的数组呢?很简单,我们只需要多加一步操作:
1 | const arr = Array.apply(null, new Array(4)); //借助apply给每一个数组的每一个index都赋值 |
2.利用数组给方法传递空参数###
我们调用方法的时候,有时想对一些参数进行忽略,比如下面的情况
1 | method('parameter1', , 'parameter3'); //会报错, Uncaught SyntaxError: Unexpected token , |
这个时候通常我们的做法是会传一个null
或者 undefined
作为占位
1 | method('parameter1', null, 'parameter3') |
我们可以借助ES6的展开语法(…)来辅助传参
1 | method(...['parameter1', , 'paramter3']); // 大功告成! |
3. 数组去重###
使用展开符号搭配set
进行数组去重
1 | const arr = [...new Set([1, 2, 3, 3])];//[1, 2, 3] |
参考来源:
3 Array Hacks