ES6 新增数组方法

1,210 阅读3分钟

Array.from

Array.from(arrayLike[, mapFn[, thisArg]])

  • 将类数组转换成数组

  • 参数:

    • arrayLike:类数组
    • mapFn:类似 map 方法,循环类数组时的回函函数,返回值组成新数组
    • thisArg: mapFn 函数执行时的 this 指向
  • 返回值:根据 arrayLike 生成的新数组

let lis = document.querySelectorAll("#list li");
let arr=[];
//方式一:
lis = Array.from(lis);
//方式二:
lis = [...lis]
console.log(lis);   //[li,li,li,li]

Array.isArray

Array.isArray(data)

  • 检测数据是否是个数组

  • data:要检测的数据

  • 返回值:true 数组,false 非数组

let lis = document.querySelectorAll("#list li");
let arr=[];
lis = Array.from(lis);
console.log(Array.isArray(lis)); //true

Array.of

Array.of(element0[, element1[, ...[, elementN]]])

  • 将参数转成一个数组

  • elementN:要放入数组中的数据

  • 返回值:新数组

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

arr.find

arr.find(callback[, thisArg])

  • 查找数组中满足要求的第一个元素的值

  • 参数:

    • callback:在数组每一项上执行的函数,接收 3 个参数:
      • element:当前遍历到的元素。
      • index:当前遍历到的索引。
      • array:数组本身
    • thisArg:执行回调时用作this 的对象
  • 返回值:数组中第一个满足所提供测试函数的元素的值,否则返回 undefined

let arr = [1,2,3,4]
let val = arr.find(item=>item>=3);
console.log(val);   //3

arr.findIndex

arr.findIndex(callback[, thisArg])

  • 查找数组中满足要求的第一个元素的值的索引

  • 参数:

    • callback:针对数组中的每个元素, 都会执行该回调函数, 执行时会自动传入下面三个参数:
      • element:当前元素。
      • index:当前元素的索引。
      • array:调用findIndex的数组。
    • thisArg:执行callback时作为this对象的值
  • 返回值:满足要求的值的索引

let arr = [1,2,3,4]
let index = arr.findIndex(item=>item>=3);
console.log(index);     //2

arr.flat

arr.flat([depth])

  • 扁平化多维数组

  • depth:指定要提取嵌套数组的结构深度,默认值为 1。

  • 返回值:一个包含将数组与子数组中所有元素的新数组

let arr = [
    ["小明","18"],
    ["小刚","18"],
    [
        [1,
            [3,4],
            [5,6],
            [
                [7],
                [8]
            ]
        ]
    ]
];
console.log(arr.flat(2));
// ["小明", "18", "小刚", "18", 1, [3, 4], [5, 6], [[7], [8]]]
console.log(arr.flat(Infinity));
// ["小明", "18", "小刚", "18", 1, 3, 4, 5, 6, 7, 8]

arr.flatMap

arr.flatMap(function callback(currentValue[, index[, array]]) { // 返回新数组的元素 }[, thisArg])

  • 方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 map 和 深度值1的 flat 几乎相同,但 flatMap 通常在合并成一种方法的效率稍微高一些

  • 参数:

    • callback:可以生成一个新数组中的元素的函数,可以传入三个参数:
      • currentValue:当前正在数组中处理的元素
      • index:数组中正在处理的当前元素的索引。
      • array:被调用的 map 数组
    • thisArg:执行 callback 函数时 使用的this 值
  • 返回值:一个包含将数组与子数组中所有元素的新数组

let arr = [
    ["小明","18"],
    ["小刚","18"]
];
let newArr = arr.flatMap((item)=>{
    item = item.filter((item,index)=>{
        return index == 0;
    });
    return item;
});
console.log(newArr);        //["小明", "小刚"]

arr.fill

arr.fill(value[, start[, end]])

  • 用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引

  • 参数:

    • value:用来填充数组元素的值。
    • start:起始索引,默认值为0。
    • end:终止索引,默认值为 arr.length
  • 返回值:当前改变后的数组

let arr = [0,1,2,3,4];
arr.fill("a",1,7);
console.log(arr);   //[0, "a", "a", "a", "a"]

arr.includes

arr.includes(valueToFind[, fromIndex])

  • 判断数组中是否包含一个指定的值

  • 参数:

    • valueToFind:需要查找的值
    • fromIndex:从 fromIndex 处开始向后查找
  • 返回值: true 代表数组中包含 valueToFind, false 代表数组中不包含 fromIndex

let arr = ["a","b","c","d","e"];
console.log(arr.includes("c",2));   //true
console.log(arr.includes("c",3));   //false