ES6学习之路5----数组方法

来源:互联网 发布:佰笛手风琴 淘宝 编辑:程序博客网 时间:2024/05/29 07:37

1. Array.from()

Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。

实例1:

let obj = {'0':1,'1':2,'2':3,length:3};ES5:console.log([].slice.call(obj));//[1, 2, 3]ES6:console.log(Array.from(obj));//[1, 2, 3]

实际应用:将DOM操作的NodeList 集合和函数的arguments对象转化为真正的数组。
实例2:

// NodeList对象let divList = document.querySelectorAll('div');let arrDivList = Array.from(divList);//开始可以对arrDivList使用所有数组的方法,例如常见的forEach循环arrDivList.forEach(current => console.log(current));// arguments对象function fn(){  let args = Array.from(arguments);  args.forEach(current => console.log(current));}fn(1,2,3);//1 2 3

将具有 Iterator 接口的数据转化为真正的数组。
实例3:

let str = 'Iterator';console.log(Array.from(str));//["I", "t", "e", "r", "a", "t", "o", "r"]let set = new Set([1,2,3]);console.log(Array.from(set));//[1, 2, 3]

2. Array.of()

Array.of方法用于将一组值,转换为数组。
Array.of方法的主要目的,是弥补数组构造函数Array()的不足。因为参数个数的不同,会导致Array()的行为有差异。

实例:

Array():console.log(Array()); // []console.log(Array(3)); // [, , ,]console.log(Array(undefined));//[undefined]console.log(Array(3, 11, 8));//[3,11,8]Array.of():console.log(Array.of()); // []console.log(Array.of(3)); // [3]console.log(Array.of(undefined));//[undefined]console.log(Array.of(3, 11, 8));//[3,11,8]

3. copyWithin

数组实例的copyWithin方法,在当前数组内部,将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。也就是说,使用这个方法,会修改当前数组。

Array.prototype.copyWithin(target, start , end );
  • target(必需):从该位置开始替换数据。
  • start(可选):从该位置开始读取数据,默认为 0。如果为负值,表示倒数。
  • end(可选):到该位置前停止读取数据,默认等于数组长度。如果为负值,表示倒数。
[1, 2, 3, 4, 5].copyWithin(0, 1, 2);//[2, 2, 3, 4, 5][1, 2, 3, 4, 5].copyWithin(0, -2, -1);//[4, 2, 3, 4, 5]

4.find() 和 findIndex()

find()

find方法,用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员(返回的是成员)。如果没有符合条件的成员,则返回undefined

[1,3,5,7,9].find(n => n > 6);//7[1,3,5,7,9].find(n => n < 1);//undefined

findIndex()

findIndex方法的用法与find方法非常类似,返回第一个符合条件的数组成员的位置(返回的是位置),如果所有成员都不符合条件,则返回-1

[1,3,5,7,9].findIndex(n => n > 6);//3[1,3,5,7,9].findIndex(n => n < 1);//-1

从上边的实例中我们发现和indexOf方法一样,那么我们接着看看不一样的:

[1,2,NaN,3].indexOf(NaN);//-1[1,2,NaN,3].indexOf(3);//3[1,2,NaN,3].find(n => Object.is(NaN, n));//NaN[1,2,NaN,3].findIndex(n => Object.is(NaN, n));//2

find和findIndex方法都可以发现NaN,弥补了数组的indexOf方法的不足。他们借助了Object.is方法查找NaN。

indexOf的缺点:
1. 不够语义化,它的含义是找到参数值的第一个出现位置,所以要去比较是否不等于-1,表达起来不够直观。
2. 它内部使用严格相等运算符(===)进行判断,这会导致对NaN的误判。

5.fill()

fill方法使用给定值,填充一个数组。

[1,2,3].fill(8);//[8, 8, 8][1,2,3].fill(8,1,2);//[1, 8, 3][1,2,3].fill(8,-2,-1);//[1, 8, 3]

6.entries(),keys()

entries(),keys()——用于遍历数组。它们都返回一个遍历器对象(详见《Iterator》一章),可以用for…of循环进行遍历,唯一的区别是keys()是对键名的遍历,entries()是对键值对的遍历。

for(let key of ['a','b','c','d']){    console.log(key);}//a b c dfor(let key of ['a','b','c','d'].keys()){    console.log(key);}//0 1 2 3for(let [value,key] of ['a','b','c','d'].entries()){    console.log(value+'----'+key);}//0----a1----b2----c3----d

7.includes()

Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似。

[1, 2, 3].includes(2);     // true[1, 2, 3].includes(4);     // false[1, 2, NaN].includes(NaN); // true

从上边实例可以看出,includes方法能够查找是否存在NaN。

原创粉丝点击