profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

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

  1. Array.prototype.findIndex()

    console.log([6, -6, 8].findIndex((x) => x < 0)) // 1
  2. Array.prototype.find()

    console.log([6, -6, 8].find((x) => x < 0)) // -6

Iteration

  1. Array.prototype.entries()

    console.log(Array.from(["a", "b"].entries())) // [ [ 0, 'a' ], [ 1, 'b' ] ]
  2. Array.prototype.values()

    console.log(Array.from(["a", "b"].values())) // ['a', 'b']
  3. 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