当前位置:首页 > ES6 > 正文内容

es6 数组扩展

自由小鸟6年前 (2019-07-26)ES63389

Array.from

Array.of

copyWithin

find/findindex

fill

entries/keys/values

inludes



{

    let arr=Array.of(3,4,7,9,11)   //把一组数据变量转换成数组,如果Array.of里面如果不放参数打印出来是一个空数组[]

}

{

    //Array.from 把一些伪数据集合转换成真正的数组

    let p=document.querySelectorAll('p')

    let pArr=Array.from(p);

    pArr.forEach(function(item){

        console.log(item.textContent);

    })


    //Array.from也可以传两个参数,每 个是数组,第二个是函数把第一个参数数组都遍历了一次

    console.log(Array.from([1,3,4],function(item){

        return item*2    

    }))   //[2,6,8]

    

}

{

    //把数组所有的值都换成一个值  es6 使用fill 

    //一个参数就是需要替换成的值

    //3个参数就是(换成的值,起始位置,结束位置)

    console.log('fill-7',[1,2,'a'].fill(7))   //[7,7,7]

     console.log('fill-pos',['a','b','c'].fill(7,1,3))   //['a',7,7]

}

{

    for(let index of ['1','c','a'].keys()){

        console.log('keys',index)   //0,1,2

    }

   for(let value of ['1','c','a'].keys()){     //有兼容问题

        console.log('keys',index)   //0,1,2

    }

   for(let [index,value] of ['1','c','ks'].entries()){     //有兼容问题

        console.log('values',index,value)   //0,1,2

    }

}

{

    //就是把某一位数据复制替换掉,[第几位数需要替换的数,从第几位开始,结束不包括位置]

    console.log([1,2,3,4,5].copyWithin(0,3,4));   //[4,2,3,4,5]      https://blog.csdn.net/qq_30100043/article/details/53219365

}


find

{

    console.log([1,2,3,4,5,6].find(function(item){

        return item>3     //4    find只找到第一个就返回了,不会再继续往下找了

    }))


     console.log([1,2,3,4,5,6].findIndex(function(item){

        return item>3     //3   返回值的下标   

    }))

}

{

    console.log('number',[1,2,NaN].includes(1));   //判断数组中是不是满足条件

    console.log('number',[1,2,NaN].includes(NaN));   //es5中找不到NaN的,在Es6 中是可以找到的

}




版权声明:本文由Web学习之路发布,如需转载请注明出处。

本文链接:https://webge.net/?id=51

“es6 数组扩展” 的相关文章

js 数据保护

js 数据保护

es3的写法利用构造函数闭包来实现属性不可编辑es5的写法用一个defineProperty 来实现只可读 writable:falsees6的实现,代理new Proxy来实现,思想和es3很像,操作是代理对象person...

es6 解构的使用场景

二,解构{    let a,b,rest;    [a,b]=[1,2]    console.log(a,b);  // 1 2}{   ...

es6 解构函数默认值

es6 解构函数默认值

1,如果函数对数解构值有默认值的情况,调用传值参数不传不会报错2,如果出现解构值没有默认值,那当传参数的时候没有传就会报错...

自己实现最基础的promise

没事自己手写了一下 let pi=new Promise((resolve,reject)=>{ resolve(100); reject(0); }) p1.then(result=>{ console.log('成功'+result...