• ES6学习笔记 —— 数组


    一、数组扩展创建

    1. Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Set和Map),可以接受第二个参数,用来对每个元素进行处理,将处理后的值放入返回的数组。

    2. Array.of方法用于将一组值,转换为数组,弥补数组构造函数Array()的不足。

    3. Array方法没有参数、一个参数、三个参数时,返回结果都不一样。只有当参数个数不少于2个时,Array()才会返回由参数组成的新数组。参数个数只有一个时,实际上是指定数组的长度

    Array.from([1, 2, 3])
    等同于
    Array.of(1, 2, 3)
    

    二、查找

    find():用于找出第一个符合条件的数组元素。

    findIndex:返回第一个符合条件的数组元素的位置,如果所有元素都不符合条件,则返回 -1 。

    let List = [1,2,3,4]
    
    List.find(item=>item>3)  //4
    List.findIndex(item=>item>3) //3
    
    let arr = [
          {id:1,name:'AAA'},
          {id:2,name:'BBB'},
          {id:3,name:'CCC'},
          {id:4,name:'DDD'}
    ] 
    
    arr.find(item=>item.id==1)  //{id:1,name:'AAA'}
    arr.findIndex(item=>item.id==1) //0
    

    三、填充

    fill():将一定范围索引的数组元素内容填充为单个指定的值。

    copyWithin():将一定范围索引的数组元素修改为此数组另一指定范围索引的元素。

    [1,2,3,4].fill(0,1,2) // [1, 0, 3, 4]
    
    [1, 2, 3, 4, 5].copyWithin(0, 3, 4)  // 将3号位复制到0号位, [4, 2, 3, 4, 5]
    

     

    四、遍历数组

    keys() :对键名的遍历

    values(): 对键值的遍历

    entries(): 对键值对的遍历

        for (let index of ['a', 'b'].keys()) {
          console.log(index);
        }
        // 0
        // 1
    for (let elem of ['a', 'b'].values()) { console.log(elem); } // 'a' // 'b'
    for (let [index, elem] of ['a', 'b'].entries()) { console.log(index, elem); } // 0 "a" // 1 "b"

    五、包含

    includes():数组是否包含指定值。

    注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找键名。

    • Map 结构的 has 方法,是用来查找键名的,比如 Map.prototype.has(key) 、 WeakMap.prototype.has(key) 、 Reflect.has(target, propertyKey) 。
    • Set 结构的 has 方法,是用来查找值的,比如 Set.prototype.has(value) 、 WeakSet.prototype.has(value) 。
        // 参数1:包含的指定值
        [1, 2, 3].includes(1);    // true
         
        // 参数2:可选,搜索的起始索引,默认为0
        [1, 2, 3].includes(1, 2); // false
         
        // NaN 的包含判断
        [1, NaN, 3].includes(NaN); // true
    

    六、数组扁平化

    flat():方法将子数组的成员取出来,添加在原来的位置,flat() 方法的参数写成一个整数,表示想要拉平的层数,默认为1。

    flatMap():方法对原数组的每个成员执行一个函数(相当于执行 Array.prototype.map() ),然后对返回值组成的数组执行 flat() 方法。该方法返回一个新数组,不改变原数组。

    [1, 2, [3, [4, 5]]].flat()  // [1, 2, 3, [4, 5]]
    
    [1, 2, [3, [4, 5]]].flat(2)  // [1, 2, 3, 4, 5]
    
    [1, [2, [3]]].flat(Infinity)  //不管有多少层嵌套,都要转成一维数组  [1, 2, 3]
    
    [1, 2, , 4, 5].flat()  // 跳过空位  [1, 2, 4, 5]
    
    // 相当于 [[2, 4], [3, 6], [4, 8]].flat()
    [2, 3, 4].flatMap((x) => [x, x * 2])  // [2, 4, 3, 6, 4, 8]
     
    // 相当于 [[[2]], [[4]], [[6]], [[8]]].flat()
     [1, 2, 3, 4].flatMap(x => [[x * 2]])  // [[2], [4], [6], [8]]
    

    七、合并数组

    [...[1, 2],...[3, 4]] //[1, 2, 3, 4]
    

    八、复制数组

    let arr = [1, 2]
    let arr1 = [...arr] // [1, 2]
         
    // 数组含空位
    let arr2 = [1, , 3]
    let arr3 = [...arr2] // [1, undefined, 3]
    
  • 相关阅读:
    ecshop 调用指定分类的推荐,热卖,新品
    ecshop 首页调用指定类产品
    html常用笔记
    ecshop 修改flash图片大小
    ecshop 删除随机版权
    Java Web(一) Servlet详解!!
    Git使用总结
    git clone命令使用
    Lucene学习总结之四:Lucene索引过程分析
    Lucene学习总结之二:Lucene的总体架构
  • 原文地址:https://www.cnblogs.com/Intellectualscholar/p/16190746.html
Copyright © 2020-2023  润新知