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 ofelem
in array, or-1
-
[].lastIndexOf(elem)
returns index of last instance ofelem
in array, or-1
-
[].filter(fn)
returns array of elements that satisfy ourfn
test -
[].find(fn)
returns element in the array that satisfies ourfn
test, orundefined
-
[].findIndex(fn)
returns index of the first element that satisfies ourfn
test, or-1
-
[].every(fn)
checks if every element in the array satisfies ourfn
test -
[].some(fn)
checks if at least one element in the array satisfies ourfn
test -
[].includes(elem)
checks if array containselem
-
[].values()
returns an iterator to loop over elements in array -
[].entries()
returns an iterator to loop over index/element pairs in array -
[].forEach(fn)
executesfn
once for each element -
[].reduce(fn)
reduce values of an array to a single value -
[].map(fn)
creates new array by calling afn
for each element -
[].flatMap()
runsmap()
followed byflat(1)
-
[].concat(arr)
joins 2 arrays -
[].join(str)
joins all elements of an array into a string delimited bystr
-
[].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 depthn
-
[].copyWithin(i1, i2)
copies sequence of elements fromi2
to the end to indexi1
onward -
[].splice(i,n)
starting fromi
, removesn
elements and returns the modified array -
[].reverse()
reverses order of the array -
[].fill(elem)
fills all elements withelem
-
[].pop()
removes and returns last element of array -
[].push(elem)
addselem
to end of array and returns length -
[].shift()
removes and returns first element of array -
[].unshift(elem)
addselem
to beginning of array and returns length -
[].sort()
sorts an array