数组的方法总结二(ES6)

194 阅读5分钟

Array.of()

Array.of().of():方法将参数中所有值作为元素形成数组。
语法:Array.of(element0, element1, /* … ,*/ elementN)

elementN:用于创建数组的元素。

返回值:返回新的Array实例。

Array.of(1);          // [1]
Array.of(1, 2, 3);   // [1, 2, 3]
Array.of(undefined); // [undefined]

Array.from()

Array.from():方法将类数组对象或可迭代对象转化为数组。
语法:Array.from(arrayLike[, mapFn[, thisArg]])

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

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

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

返回值:一个新的数组实例。

// 参数含空位 
console.log(Array.from([1, , 3])); // [1, undefined, 3]

const set = new Set(['www', 'jjj', 'jjj', 'kkk']);
Array.from(set);
// [ "www", "jjj", "kkk" ]

const map = new Map([[1, 2], [3, 4], [5, 6]]);
Array.from(map);
// [[1, 2], [3, 4], [5, 6s]]

find()

find():查找数组中符合条件的元素,若有多个符合条件的元素,则返回第一个元素。
语法:Array.find(function(currentValue, index, arr),thisValue)

callbackFn:在数组每一项上执行的函数,接收 3 个参数:

currentValue:数组中正在处理的当前元素。

index(可选):当前元素的索引值。

arr(可选):当前元素所在的数组对象。

thisValue(可选):传递给函数的值一般用 "this" 值。

返回值:数组中第一个满足所提供测试函数的元素的值,否则返回 undefined

let arr1 = [1, 2, 3, 4, 5];
let num = arr1.find(item => item > 2);
console.log(num)  //3

findIndex()

findIndex():方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。
语法:array.findIndex(function(currentValue, index, arr), thisValue)

callbackFn:针对数组中的每个元素,都会执行该回调函数,执行时会自动传入下面三个参数:

currentValue:数组中正在处理的当前元素。
index(可选):当前元素的索引值。
arr(可选):当前元素所在的数组对象。
thisValue(可选):传递给函数的值一般用 "this" 值,如果这个参数为空, undefined 会传递给 this

返回值:数组中通过提供测试函数的第一个元素的索引。否则,返回 -1

const arr = ['a','b','c','d'];
   let flag = arr.findIndex(item => {
        return item === 'c';
    })
    console.log(flag) //2

fill()

fill():将一定范围索引的数组元素内容填充为单个指定的值,不包括终止索引。
语法:fill(value, start, end)

value:用来填充数组元素的值。

start(可选):起始索引,默认值为 0。

end(可选):终止索引,默认值为 arr.length

返回值:修改后的数组。

const arr = [1, 2, 3] ;
arr.fill(4, 1, 2);// [1, 4, 3]

copyWithin()

copyWithin():将一定范围索引的数组元素修改为此数组另一指定范围索引的元素,不包括终止索引。
语法:copyWithin(target, start, end)

target:被修改的起始索引。

start(可选):被用来覆盖的数据的起始索引。

end(可选):被用来覆盖的数据的结束索引,默认为数组末尾,- 如果 end 被忽略,copyWithin 方法将会一直复制至数组结尾(默认为 arr.length)。

返回值:改变后的数组。

let s = [1, 2, 3, 4, 5].copyWithin(-2); //copyWithin(-2, 0, 5)
      console.log(s);
      // [1, 2, 3, 1, 2]

entries()

entries():遍历键值对。
语法:entries()

返回值:一个新的Array迭代器对象。

const a = ["a", "b", "c"];

for (let [index, ele] of a.entries()) {
    console.log(index, ele);
}
// 0 'a'
// 1 'b'
// 2 'c'

keys()

keys():遍历键名。
语法:keys()

返回值:一个新的Array迭代器对象。

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

// 数组含空位
console.log([...[,'a'].keys()]); // [0, 1]

values()

values():遍历键值。
语法:values()

返回值:一个新的Array迭代对象。

const arr = ['a', 'b', 'c', 'd', 'e'];
const iterator = arr.values();

for (let val of iterator) {
  console.log(val);
}  
//"a" "b" "c" "d" "e"

includes()

includes():方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false
注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值Map 的 has 方法用于查找键名。
语法:includes(searchElement, fromIndex)

searchElement:需要查找的元素值。

fromIndex(可选):设置从那个位置开始查找,默认为 0。 如果 fromIndex 为负值,计算出的索引将作为开始搜索searchElement的位置。如果计算出的索引小于 0,则整个数组都会被搜索。

返回值:返回布尔值。

const arr = [1, 2, 3];
console.log(arr.includes(2));
// true

const arr = ['a', 'b', 'c'];
arr.includes('a', -100); 
// true

flat()

flat():方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。 语法:flat(depth)

depth(可选):指定要提取嵌套数组的结构深度,默认值为 1。 返回值:一个包含将数组与子数组中所有元素的新数组。

const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat(2);
// [1, 2, 3, 4, 5, 6]

flatMap()

flatMap():方法它会遍历原数组的每一个元素, 并且会为每个元素都执行一次传入的回调函数,最终把所有元素执行回调函数返回的结果压缩成一个新数组,flatMap会返回一个新的数组,不会改变原数组的元素。 语法:Array.flatMap(callback(currentValue),thisArg)

callback:可以生成一个新数组中的元素的函数,可以传入三个参数:

currentValue:当前正在数组中处理的元素。

index(可选):可选的。数组中正在处理的当前元素的索引。

array(可选):被调用的 map 数组 ]thisArg(可选):执行 callback 函数时 使用的this 值。

返回值:一个新的数组,其中每个元素都是回调函数的结果,并且结构深度 depth 值为 1。

  const arr = [1, 2, 3, 4, 5];
  const a1 = arr.flatMap((x) => [x ** 2]);
  console.log(a1); // [1, 4, 9, 16, 25]
  const a2 = arr.map((x) => [x ** 2]);
  console.log(a2); // [[1],[4],[9],[16],[25]]
  console.log(a2.flat()); // [1, 4, 9, 16, 25]