mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 11:36:23 +00:00
2.2 KiB
2.2 KiB
date | id | title |
---|---|---|
2020-10-09 | c76b3766-a1ed-43f9-bdb6-4076e6bb5b7a | JavaScript Array Methods |
ES6
Static Array methods
Array.from()
const arr2 = Array.from(arguments); // ES6
If a value is iterable (as all Array-like DOM data structure are by now), you can also use the spread operator (…) to convert it to an Array:
const arr1 = [...'abc'];
// ['a', 'b', 'c']
const arr2 = [...new Set().add('a').add('b')];
// ['a', 'b']
Array.of()
This returns an array of the passed parameters
console.log(Array.of(1, 2, 3, 4)) // [1, 2, 3, 4]
Array.prototype methods
Array.prototype.fill()
const arr2 = new Array(2).fill(undefined);
// [undefined, undefined]
Array.prototype.copyWithin()
The method signature is:
Array.prototype.copyWithin(target : number,
start : number, end = this.length) : This
It copies the elements whose indices are in the range [start,end) to index target and subsequent indices. If the two index ranges overlap, care is taken that all source elements are copied before they are overwritten. I am confused as to how this is in any way useful.
const arr = [0,1,2,3];
console.log(arr.copyWithin(2, 0, 2)) // [0, 1, 0, 1]
Searching for elements
-
Array.prototype.findIndex()
console.log([6, -6, 8].findIndex(x => x < 0)) // 1
-
Array.prototype.find()
console.log([6, -6, 8].find(x => x < 0)) // -6
Iteration
-
Array.prototype.entries()
console.log(Array.from(['a', 'b'].entries())) // [ [ 0, 'a' ], [ 1, 'b' ] ]
-
Array.prototype.values()
console.log(Array.from(['a', 'b'].values())) // ['a', 'b']
-
Array.prototype.keys()
console.log(Array.from(['a', 'b'].keys())) // [0, 1]
ES2016
Array.prototype.includes()
Tells you if array includes a certain element:
console.log(["a", "b", "c"].includes("a")); // true
console.log(["a", "b", "c"].includes("d")); // false