ES6语法(5)

数组扩展


1.Array.of  把一组数据转变为数组

        {
            let arr = Array.of(1,3,4,5,6);
            console.log(arr);   // [1,3,4,5,6]
        }


2.Array.from 

当只有一个参数的时,把伪数组转化为数组

当有两个参数时  console.log(Array.from([1,2,3],function(item){ return item*2}));   //[2,4,6] 遍历数组,执行操作


3. fill

把数组里每个位置换成同一个值

        {
            console.log([1,2,undefined].fill(7));   // [7,7,7]
        }

从起始位置到结束位置之间的每个值都替换成规定的值(包左不包右)

        {
            console.log([1,2,undefined].fill(7,1,3));   // [1,7,7]
        }


4.keys

        {
            var arr = [3,2,1];
            for(let index of arr.keys()){
                console.log(index);   //0,1,2  数组的索引值
            }
        }


5.values 不开兼容模式,浏览器不支持

        {
            var arr = ["a","b","c"];
            for(let value of arr.values()){
                console.log(value );  //  a  b  c
            }
        }


6.entries

        {
            for(let [index,value] of ["1","c","ks"].entries()){
                console.log(index,value);   index表示索引值   value表示值
            }
        }

7.copyWithin

        {
            console.log([1,2,3,4,5,6].copyWithin(0,2,4));   //从0开始,读取2-4之间的数据(包左不包右),从0开始替换
        }

结果是[3,4,3,4,5,6]


8.find

找到符合要求的第一个值

        {
            console.log([1,2,3,4,5,6].find(function(item){ return item>3}));  //4
        }


9.findIndex

找到符合要求的第一个值的索引

        {
            console.log([1,2,3,4,5,6].find(function(item){ return item>3}));  //3
        }


10.includes

数组里是否包含某个值,包括NaN

        {
            console.log([1,2,NaN].includes(NaN));  //true
        }

        {
            console.log([1,2,NaN].includes(1));  //true
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值