• es6-数组的扩展


    1.Array.of:将一组数据变量,转化成为数据类型

    let arr=Array.of(3,2,4,5,55);

    console.log('arr=',arr);

    输出为

    这里就返回了一个数组

    如果里面不放任何的参数,那么返回的是一个空数组

    2.Array.from():将伪数组转换为数组,进行map映射rom

    { let p=document.querySelectorAll('p');

    let pArr=Array.from(p);

    pArr.forEach(function(item){ console.log(item.textContent);

    //这是js的一个获取dom节点的一个方法 }) }

    输出为1,2,3

    映射:将数组里的数字都遍历,*2再输出

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

    输出为2,4,6

    3.fill:将数组中的所有值换成相同的一个数字,还可以设定范围的替换

    (1)

    console.log('fill-7:',['1',2,'a',undefined].fill(7));

    (2)

    console.log('fill.pos:',['1',2,'a',undefined].fill(7,1,3));

    这里fill(7,1,3)中‘7’是要替换成的数值,‘1’是起始数(0开始计数),‘3’是结束位置,但是不会被替换掉

    4.keys:返回数组下标的集合

    for(let index of ['1',2,'k'].keys()){ console.log('keys',index); }

    5.values:返回数组的值

    for(let values of ['1',2,'k'].values()){ console.log('keys',values); }

    6,entries():返回数组的下标和值,还可以解决兼容问题

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

    7.copyWithin():将数组中的数复制到另一个位置

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

    copyWithin(0,3,5)中第一个参数0表示要被赋值的起始位置,及数组中的数字1的位置,3为要被复制的数的起始位置,及数组中的数字4,5表示要被复制的数的结束位置,但是不取5位置的数字,只取到4位置的数字,也就是数组中的数字5,将4,5数字复制到0,1的位置

    8.find():查找

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

    输出为4

    这里找数组中数值大于3的数,但find只需找到一个满足条件的就行,不会去找5,6

    9.findIndex:查找下标

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

    输出为3

    即数字4的下标

    includes():看数组中是否包含某数

    console.log('number',[1,2,NaN].includes(1));

    console.log('number',[1,2,NaN].includes(NaN));

    这里还能够找出NaN

  • 相关阅读:
    对健康的一些思考
    对提问的一些思考
    UVA 10118 Free Candies(免费糖果)(dp记忆化搜索)
    UVA 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp记忆化搜索)
    UVA 12186 Another Crisis(工人的请愿书)(树形dp)
    UVA 10003 Cutting Sticks(切木棍)(dp)
    UVA 11584 Partitioning by Palindromes(划分成回文串)(dp)
    【洛谷P1144】最短路计数【最短路】
    【洛谷P1144】最短路计数【最短路】
    【洛谷P1144】最短路计数【最短路】
  • 原文地址:https://www.cnblogs.com/ellen-mylife/p/11082971.html
Copyright © 2020-2023  润新知