1.includes()
[1, 2, 3].includes(2)
indexof要去比较是否不等于-1,表达起来不够直观;而且它内部使用严格相等运算符(===)进行判断,这会导致NaN不等于NaN
4.扩展运算符
console.log(...[1, 2, 3]) // // 1 2 3
主要用于函数调用:
// ES5 的写法
Math.max.apply(null, [14, 3, 77])
// ES6 的写法
Math.max(...[14, 3, 77])
优化某些方法:
// ES5
Array.prototype.push.apply(arr1, arr2);
// ES6
arr1.push(...arr2);
深拷贝数组:
const a1 = [1, 2];
// 写法一
const a2 = [...a1];
// 写法二
const [...a2] = a1;
3.Array.from()将伪数组对象转换为数组
常见伪数组对象:
// arguments对象
function foo() {
const args = [ ...arguments ]
}
// NodeList对象
[ ...document.querySelectorAll('div') ]
// ES5的写法
var arr1 = [].slice.call(arrayLike)
// ES6的写法
let arr2 = Array.from(arrayLike)
4.find(fn)返回符号条件的第一个元素
findIndex(fn)返回符号条件的第一个元素的索引
[1, 4, -5, 10].find((item) => {
return item < 0
})
[1, 4, -5, 10].findIndex((item) => {
return item < 0
})