ES6-----学习系列六(数组扩展)

在数组的扩展上感觉新增了很多实用的特性,并且感觉还挺重要的

  一、Array.of() 将数据变量转化成数组形式

{
  let arr = Array.of(3,4,7,9,11);
  console.log('arr=',arr);//[3,4,7,9,11]

  let empty=Array.of();//[]
  console.log('empty',empty);
}

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

    同时 Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。

{
  let p=document.querySelectorAll('p');
  let pArr=Array.from(p);
  pArr.forEach(function(item){
    console.log(item.textContent);
  });

  console.log(Array.from([1,3,5],function(item){return item*2}));//[2,6,10]
}

  三、fill(data,startIndex,endIndex-1)对数组进行填充 ,如果只有一个data参数将全部进行替换,如果三个参数将从startIndex~endIndex-1全部替换为data

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

  四、keys(返回所有的数组下标)  values(返回数组所有的value)  entries(包括所有的key和values) 

{
  for(let index of ['1','c','ks'].keys()){
    console.log('keys',index);
  }
  for(let value of ['1','c','ks'].values()){
    console.log('values',value);
  }
  for(let [index,value] of ['1','c','ks'].entries()){
    console.log('values',index,value);
  }
}

  五、copyWithin(p1,p2,p3) 从p1位置开始 用p2到p3-1的位置的数据进行覆盖

{
  console.log([1,2,3,4,5].copyWithin(0,3,4));//[4,2,3,4,5]
}

  六、find() 找到第一个符合条件的值即停止  和findIndex() 找到第一个符合条件的值的索引即停止

{
  console.log([1,2,3,4,5,6].find(function(item){return item>3}));//4
  console.log([1,2,3,4,5,6].findIndex(function(item){return item>3}));
}
{
  console.log('number',[1,2,NaN].includes(1));//true
  console.log('number',[1,2,NaN].includes(NaN));//true
}

 

转载于:https://www.cnblogs.com/diasa-fly/p/7002135.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值