es6 的数组的扩展

导读:本篇文章讲解 es6 的数组的扩展,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

// es6 数组的扩展
{
  // 将一组数转为数组
  let arr = Array.of(3,4,7,9,11);
  console.log('arr=',arr); // arr=[3,4,7,9,11]

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

// 把一些集合、伪数组转换成真正的数组
{
  let p=document.querySelectorAll('p'); // 取到页面上的所有p标签
  let pArr=Array.from(p);
  pArr.forEach(function(item){
    console.log(item.textContent); // 将页面上p标签内的内容转换成数组
  });

  console.log(Array.from([1,3,5],function(item){return item*2})); //  [2, 6, 7]
}
// 替换
{
  console.log('fill-7',[1,'a',undefined].fill(7)); // fill-7  [7, 7, 7]
  // fill(7,1,3)  7 表示要替换成的文字, 1 表示起始位置, 3 表示结束位置, 将1到3的内容换成7
  console.log('fill,pos',['a','b','c'].fill(7,1,3)); // fill,pos  ['a', 7, 7]
}

{
  // 返回所有数组的下标
  for(let index of ['1','c','ks'].keys()){
    console.log('keys',index); //  keys  0
                                 //  keys  1
                                 //  keys  2
  }
  for(let value of ['1','c','ks'].values()){
    console.log('values',value);  //  values  1
                                    //  values  c
                                    //  values  ks
  }
  for(let [index,value] of ['1','c','ks'].entries()){
    console.log('values',index,value);
                                          //  values  0  1
                                          //  values  1  c
                                          //  values  2  ks
  }
}

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

{
  // 返回符合条件的一个值
  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})); // 3
}

// 判断是否包含
{
  console.log('number',[1,2,NaN].includes(1)); // number true
    // 在es5 中会报错,因为不能处理NaN
  console.log('number',[1,2,NaN].includes(NaN)); // number true  
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/66461.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!