ES6为数组做的扩展

1.Array.of()函数: 将一组值,转化成数组

console.log(Array.of(1,2,3,4,5,6))  

2.Array.from( )函数:可将类似数组的对象或者可遍历的对象转换成真正的数组

let tag = document.getElementsByTagName('div');
console.log(tag instanceof Array);  //结果:false,不是数组
console.log(Array.from(tag) instanceof Array); //结果:true,是数组
let str = 'hello';
Array.from(str); // ['h','e','l','l','o']

3.find( )函数: 找出数组中符合条件的第一个元素

 let arr = [2,5,8,7,6,10];
    arr.find(function(value){
        return value > 2;
    });
let arr = [1,2,3,4,5,6];
arr.find(function(value){
   return value > 7;
})

4.findIndex( )函数: 返回符合条件的第一个数组成员的位置

 let arr = [7,8,9,10];
 arr.findIndex(function(value){
    return value > 8;  //  2 返回的是第一个的下标位置
 });

5.fill( )函数: 用指定的值,填充到数组

let arr = [1,2,3];
arr.fill(4);  //结果:[4,4,4]

猜你喜欢

转载自www.cnblogs.com/linjiu0505/p/11814020.html