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 ofelemin array, or-1 -
[].lastIndexOf(elem)returns index of last instance ofelemin array, or-1 -
[].filter(fn)returns array of elements that satisfy ourfntest -
[].find(fn)returns element in the array that satisfies ourfntest, orundefined -
[].findIndex(fn)returns index of the first element that satisfies ourfntest, or-1 -
[].every(fn)checks if every element in the array satisfies ourfntest -
[].some(fn)checks if at least one element in the array satisfies ourfntest -
[].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)executesfnonce for each element -
[].reduce(fn)reduce values of an array to a single value -
[].map(fn)creates new array by calling afnfor 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 fromi2to the end to indexi1onward -
[].splice(i,n)starting fromi, removesnelements 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)addselemto end of array and returns length -
[].shift()removes and returns first element of array -
[].unshift(elem)addselemto beginning of array and returns length -
[].sort()sorts an array