ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JS:ES6数组新增方法

2022-07-12 13:03:34  阅读:83  来源: 互联网

标签:ES6 arr console log JS let 数组 Array


数组

(1)数组创建

  • Array.of():将参数中所有值作为元素形成数组。

console.log(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4]

// 参数值可为不同类型
console.log(Array.of(1, '2', true)); // [1, '2', true]

// 参数为空时返回空数组
console.log(Array.of()); // []

 

 

  • Array.from():将类数组对象或可迭代对象转化为数组。

    // 参数为数组,返回与原数组一样的数组
    console.log(Array.from([1, 2])); // [1, 2]
     
    // 参数含空位
    console.log(Array.from([1, , 3])); // [1, undefined, 3]

     

    参数说明:Array.from(arrayLike[, mapFn[, thisArg]])

    • arrayLike:想要转换的类数组对象或可迭代对象。

    console.log(Array.from([1, 2, 3])); // [1, 2, 3]

     

    • mapFn:可选,map 函数,用于对每个元素进行处理,放入数组的是处理后的元素。

    console.log(Array.from([1, 2, 3], (n) => n * 2)); // [2, 4, 6]

     

    • thisArg:可选,用于指定 map 函数执行时的 this 对象。

    let map = {
        do: function(n) {
            return n * 2;
        }
    }
    let arrayLike = [1, 2, 3];
    console.log(Array.from(arrayLike, function (n){
        return this.do(n);
    }, map)); // [2, 4, 6]

     

  • 类数组对象:一个类数组对象必须含有 length 属性,且元素属性名必须是数值或者可转换为数值的字符。

let arr = Array.from({
 0: '1',
 1: '2',
 2: 3,
 length: 3
});
console.log(); // ['1', '2', 3]

// 没有 length 属性,则返回空数组
let array = Array.from({
 0: '1',
 1: '2',
 2: 3,
});
console.log(array); // []

// 元素属性名不为数值且无法转换为数值,返回长度为 length 元素值为 undefined 的数组  
let array1 = Array.from({
 a: 1,
 b: 2,
 length: 2
});
console.log(array1); // [undefined, undefined]

 

 

  • 转换可迭代对象

    • 转换map

    let map = new Map();
    map.set('key0', 'value0');
    map.set('key1', 'value1');
    console.log(Array.from(map)); // [['key0', 'value0'],['key1','value1']]

     

    • 转换set

    let arr = [1, 2, 3];
    let set = new Set(arr);
    console.log(Array.from(set)); // [1, 2, 3]

     

    • 转换字符串

    let str = 'abc';
    console.log(Array.from(str)); // ["a", "b", "c"]

     

(2)扩展的方法

  • 查找:

    • find():查找数组中符合条件的元素,若有多个符合条件的元素,则返回第一个元素。

    let arr = Array.of(1, 2, 3, 4);
    console.log(arr.find(item => item > 2)); // 3
     
    // 数组空位处理为 undefined
    console.log([, 1].find(n => true)); // undefined

     

    • findIndex():查找数组中符合条件的元素索引,若有多个符合条件的元素,则返回第一个元素索引。

    let arr = Array.of(1, 2, 1, 3);
    // 参数1:回调函数
    // 参数2(可选):指定回调函数中的 this 值
    console.log(arr.findIndex(item => item = 1)); // 0
     
    // 数组空位处理为 undefined
    console.log([, 1].findIndex(n => true)); //0

     

  • 填充:

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

    let arr = Array.of(1, 2, 3, 4);
    // 参数1:用来填充的值
    // 参数2:被填充的起始索引
    // 参数3(可选):被填充的结束索引,默认为数组末尾
    console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]

     

  • 遍历:

    • entrys():遍历键值对。

    for(let [key, value] of ['a', 'b'].entries()){
        console.log(key, value);
    }
    // 0 "a"
    // 1 "b"
     
    // 不使用 for... of 循环
    let entries = ['a', 'b'].entries();
    console.log(entries.next().value); // [0, "a"]
    console.log(entries.next().value); // [1, "b"]
     
    // 数组含空位
    console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]

     

    • keys():遍历键名。

    for(let key of ['a', 'b'].keys()){
        console.log(key);
    }
    // 0
    // 1
     
    // 数组含空位
    console.log([...[,'a'].keys()]); // [0, 1]

     

    • values():遍历键值

    for(let value of ['a', 'b'].values()){
        console.log(value);
    }
    // "a"
    // "b"
     
    // 数组含空位
    console.log([...[,'a'].values()]); // [undefined, "a"]

     

  • 包含:

    • includes():数组是否包含指定值。注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找键名。

    // 参数1:包含的指定值
    [1, 2, 3].includes(1);    // true
     
    // 参数2:可选,搜索的起始索引,默认为0
    [1, 2, 3].includes(1, 2); // false
     
    // NaN 的包含判断
    [1, NaN, 3].includes(NaN); // true

     

  • 嵌套数组转一维数组:

    • flat()

    console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
     
    // 指定转换的嵌套层数
    console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]
     
    // 不管潜逃多少层
    console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
     
    // 自动跳过空位
    console.log([1, [2, , 3]].flat());<p> // [1, 2, 3]

     

(3)扩展运算符(...)

  • 复制数组:

let arr = [1, 2],
   arr1 = [...arr];
console.log(arr1); // [1, 2]

// 数组含空位
let arr2 = [1, , 3],
   arr3 = [...arr2];
console.log(arr3); [1, undefined, 3]

 

  • 合并数组:

console.log([...[1, 2],...[3, 4]]); // [1, 2, 3, 4]

 

 

标签:ES6,arr,console,log,JS,let,数组,Array
来源: https://www.cnblogs.com/LIXI-/p/16468473.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有