JavaScript Arrays Cheat Sheet

Static Properties

Array.from('123'); // ['1','2','3']
Array.isArray([1,2,3]); // true
Array.of(1,2,3) // [1,2,3]

Instance Properties
Search or run test on array

[1,2,2,3].indexOf(2); // 1
[1,2,2,3].lastIndexOf(2); // 2
[1,2,2,3].filter(n => n === 2); // [2,2]
[1,2,2,3].find(n => n === 2); // 2
[1,2,2,3].findIndex(n => n % 2 === 0); // 1
[1,2,2,3].every(n => n % 2 === 0); // false
[1,2,2,3].some(n => n % 2 === 0); // true
[1,2,2,3].includes(4); // false

Loop through array

for (const value of [4,5,6].values())
  console.log(value); // 4 → 5 → 6
for (const [i,n] of [4,5,6].entries())
  console.log(i,n); // 0 4 → 1 5 → 2 6
[4,5,6].forEach((n,i) => console.log(i,n)); // 0 4 → 1 5 → 2 6
[4,5,6].reduce((acc,cur) => acc + cur, 0); // 15
[4,5,6].map(n => n + 1); // [5,6,7]
[4,5,6].flatMap(n => [n + 1]); // [5,6,7]
[4,5,6].flatMap(n => [[n + 1]]); // [[5],[6],[7]]

Return new array

[1,2,3].concat([4]); // [1,2,3,4]
[1,2,3].join(' + '); // '1 + 2 + 3'
[1,2,3].slice(1,2); // [2]
[1,2,3].slice(); // [1,2,3]
[1,2,3].toString(); // '1,2,3'
[1,2,[3,4]].flat(); // [1,2,3,4]
[1,2,[[3,4]]].flat(1); // [1,2,[3,4]]
[1,2,[[3,4]]].flat(2); // [1,2,3,4]

Modify original array

[1,2,3,4].copyWithin(2,0); // [1,2,1,2]
[1,2,3].splice(1,2); // [2,3]
[1,2,3].reverse(); // [3,2,1]
[1,2,3].fill(4); // [4,4,4]
[1,2,3].pop(); // [1,2]
[1,2,3].push(4); // [1,2,3,4]
[1,2,3].shift(); // 1
[1,2,3].unshift(4); // 4
[2,3,1].sort(); // [1,2,3]

Explanations
In these examples, i refers to an index.

Array.from(iter) creates array from an iterable object

Array.isArray(arr) checks for an array

Array.of(arr) creates a new array with provided array

[].indexOf(elem) returns index of first instance of elem in array, or -1

[].lastIndexOf(elem) returns index of last instance of elem in array, or -1

[].filter(fn) returns array of elements that satisfy our fn test

[].find(fn) returns element in the array that satisfies our fn test, or undefined

[].findIndex(fn) returns index of the first element that satisfies our fn test, or -1

[].every(fn) checks if every element in the array satisfies our fn test

[].some(fn) checks if at least one element in the array satisfies our fn test

[].includes(elem) checks if array contains elem

[].values() returns an iterator to loop over elements in array

[].entries() returns an iterator to loop over index/element pairs in array

[].forEach(fn) executes fn once for each element

[].reduce(fn) reduce values of an array to a single value

[].map(fn) creates new array by calling a fn for each element

[].flatMap() runs map() followed by flat(1)

[].concat(arr) joins 2 arrays

[].join(str) joins all elements of an array into a string delimited by str

[].slice(i1,i2) returns new array from [i1, i1+i2)

[].slice() copies an array

[].toString() converts array to string

[].flat(n) flattens all sub-array elements up to a specified, zero-indexed depth n

[].copyWithin(i1, i2) copies sequence of elements from i2 to the end to index i1 onward

[].splice(i,n) starting from i, removes n elements and returns the modified array

[].reverse() reverses order of the array

[].fill(elem) fills all elements with elem

[].pop() removes and returns last element of array

[].push(elem) adds elem to end of array and returns length

[].shift() removes and returns first element of array

[].unshift(elem) adds elem to beginning of array and returns length

[].sort() sorts an array

Reference

Link

Leave a Reply

Your email address will not be published. Required fields are marked *