1.扩展运算符:扩展运算符(spread)是三个点(...
)。它比如 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。javascript
console.log(...[1, 2, 3]) // 1 2 3 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5
用于函数调用java
function add(x, y) { return x + y; } const numbers = [4, 38]; add(...numbers) // 42
扩展运算符后面还能够放置表达式。算法
const arr = [ ...(x > 0 ? ['a'] : []), 'b', ];
经过push
函数,将一个数组添加到另外一个数组的尾部。数组
let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; arr1.push(...arr2);
扩展运算法的应用数据结构
(1)复制数组函数
const a1 = [1, 2]; const a2 = a1; a2[0] = 2; a1 // [2, 2]
上面代码中,a2
并非a1
的克隆,而是指向同一份数据的另外一个指针。修改a2
,会直接致使a1
的变化。this
const a1 = [1, 2]; // 写法一 const a2 = [...a1]; // 写法二 const [...a2] = a1;上面的两种写法,
a2
都是
a1
的克隆。
(2)合并数组spa
const arr1 = ['a', 'b']; const arr2 = ['c']; const arr3 = ['d', 'e']; // ES5 的合并数组 arr1.concat(arr2, arr3); // [ 'a', 'b', 'c', 'd', 'e' ] // ES6 的合并数组 [...arr1, ...arr2, ...arr3] // [ 'a', 'b', 'c', 'd', 'e' ]
(3)与解构赋值结合prototype
// ES5 a = list[0], rest = list.slice(1) // ES6 [a, ...rest] = list
(4)字符串指针
[...'hello'] // [ "h", "e", "l", "l", "o" ]
2.Array.form()
Array.from
方法用于将两类
对象转为真正的数组
:相似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; // ES5的写法 var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c'] // ES6的写法 let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
3.Array.of()
Array.of
方法用于将一组值,转换为数组。
Array.of(3, 11, 8) // [3,11,8] Array.of(3) // [3] Array.of(3).length // 1
4.数组实例的find()和findIndex()
数组实例的find
方法,用于找出第一个符合条件的数组成员。它的参数是一个回调函数,全部数组成员依次执行该回调函数,直到找出第一个返回值为true
的成员,而后返回该成员。若是没有符合条件的成员,则返回undefined
。
[1, 4, -5, 10].find((n) => n < 0) // -5数组实例的
findIndex
方法的用法与
find
方法很是相似,返回第一个符合条件的数组成员的位置,若是全部成员都不符合条件,则返回
-1
。
[1, 5, 10, 15].findIndex(function(value, index, arr) { return value > 9; }) // 2
这两个方法均可以接受第二个参数,用来绑定回调函数的this
对象。
function f(v){ return v > this.age; } let person = {name: 'John', age: 20}; [10, 12, 26, 15].find(f, person); // 26
5.数组实例的fill()
fill
方法使用给定值,填充一个数组。
['a', 'b', 'c'].fill(7) // [7, 7, 7] new Array(3).fill(7) // [7, 7, 7]
fill
方法还能够接受第二个和第三个参数,用于指定填充的起始位置和结束位置。
['a', 'b', 'c'].fill(7, 1, 2) // ['a', 7, 'c']
6.数组实例的 entries(),keys()和values()
ES6 提供三个新的方法——entries()
,keys()
和values()
——用于遍历数组。它们都返回一个遍历器对象,能够用for...of
循环进行遍历,惟一的区别是keys()
是对键名的遍历、values()
是对键值的遍历,entries()
是对键值对的遍历。
for (let index of ['a', 'b'].keys()) { console.log(index); } // 0 // 1 for (let elem of ['a', 'b'].values()) { console.log(elem); } // 'a' // 'b' for (let [index, elem] of ['a', 'b'].entries()) { console.log(index, elem); } // 0 "a" // 1 "b"
7.数组实例的includes()
Array.prototype.includes
方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的
includes
方法相似。
[1, 2, 3].includes(2) // true [1, 2, 3].includes(4) // false [1, 2, NaN].includes(NaN) // true
8.数组实例的flat()和flatMap()
数组的成员有时仍是数组,Array.prototype.flat()
用于将嵌套的数组“拉平”,变成一维的数组。该方法返回一个新数组,对原数据没有影响。
[1, 2, [3, 4]].flat() // [1, 2, 3, 4]
flat()
默认只会“拉平”一层,若是想要“拉平”多层的嵌套数组,能够将flat()
方法的参数写成一个整数,表示想要拉平的层数,默认为1。
[1, 2, [3, [4, 5]]].flat() // [1, 2, 3, [4, 5]] [1, 2, [3, [4, 5]]].flat(2) // [1, 2, 3, 4, 5]
若是无论有多少层嵌套,都要转成一维数组,能够用Infinity
关键字做为参数。
[1, [2, [3]]].flat(Infinity) // [1, 2, 3]
flatMap()
方法对原数组的每一个成员执行一个函数(至关于执行Array.prototype.map()
),而后对返回值组成的数组执行flat()
方法。该方法返回一个新数组,不改变原数组。
// 至关于 [[2, 4], [3, 6], [4, 8]].flat() [2, 3, 4].flatMap((x) => [x, x * 2]) // [2, 4, 3, 6, 4, 8]
9.数组的空位
Array(3) // [, , ,]空位不是
undefined
,一个位置的值等于
undefined
,依然是有值的。
ES5 对空位的处理,已经很不一致了,大多数状况下会忽略空位。
forEach()
, filter()
, reduce()
, every()
和some()
都会跳过空位。map()
会跳过空位,但会保留这个值join()
和toString()
会将空位视为undefined
,而undefined
和null
会被处理成空字符串。// forEach方法 [,'a'].forEach((x,i) => console.log(i)); // 1 // filter方法 ['a',,'b'].filter(x => true) // ['a','b'] // every方法 [,'a'].every(x => x==='a') // true // reduce方法 [1,,2].reduce((x,y) => x+y) // 3 // some方法 [,'a'].some(x => x !== 'a') // false // map方法 [,'a'].map(x => 1) // [,1] // join方法 [,'a',undefined,null].join('#') // "#a##" // toString方法 [,'a',undefined,null].toString() // ",a,,"
ES6 则是明确将空位转为undefined
。
Array.from
方法会将数组的空位,转为undefined
,也就是说,这个方法不会忽略空位。
Array.from(['a',,'b']) // [ "a", undefined, "b" ]