zoukankan      html  css  js  c++  java
  • ES6为数组做的扩展

    1.Array.of()函数: 将一组值,转化成数组

    console.log(Array.of(1,2,3,4,5,6))  

    2.Array.from( )函数:可将类似数组的对象或者可遍历的对象转换成真正的数组

    let tag = document.getElementsByTagName('div');
    console.log(tag instanceof Array);  //结果:false,不是数组
    console.log(Array.from(tag) instanceof Array); //结果:true,是数组
    let str = 'hello';
    Array.from(str); // ['h','e','l','l','o']
    

      

    3.find( )函数: 找出数组中符合条件的第一个元素

     let arr = [2,5,8,7,6,10];
        arr.find(function(value){
            return value > 2;
        });
    let arr = [1,2,3,4,5,6];
    arr.find(function(value){
       return value > 7;
    })

    4.findIndex( )函数: 返回符合条件的第一个数组成员的位置

     let arr = [7,8,9,10];
     arr.findIndex(function(value){
        return value > 8;  //  2 返回的是第一个的下标位置
     });

    5.fill( )函数: 用指定的值,填充到数组

    let arr = [1,2,3];
    arr.fill(4);  //结果:[4,4,4]

    let arr = [1,2,3];
    arr.fill(4,1,3); //结果:[1,4,4],从第一个位置到第三个位置前,填充4

    6.entries( )函数:对数组的键值对进行遍历,返回一个遍历器,可以用for..of对其进行遍历。

    for(let [i,v] of ['a', 'b'].entries())
    {
       console.log(i,v);
    }
    

    7.keys( )函数:对数组的索引键进行遍历,返回一个遍历器。

    for(let index of ['a', 'b'].keys())
    {
          console.log(index);  //  结果:0,1
    }
    

    8.values( )函数:数组的元素进行遍历,返回一个遍历器。

    for(let value of ['a', 'b'].values())
    {
       console.log(value);
    }
    
  • 相关阅读:
    hadoop_并行写操作思路_2
    hadoop_并行写操作思路
    Hadoop_Block的几种状态_DataNode
    KMP算法_读书笔记
    德才论
    换个格式输出整数
    继续(3n+1)猜想
    害死人不偿命的(3n+1)猜想
    c# number求和的三种方式
    c# 中的协变和逆变
  • 原文地址:https://www.cnblogs.com/linjiu0505/p/11814020.html
Copyright © 2011-2022 走看看