Cheatsheet for a bunch of array methods I forget all the time - what did unshift do again?
StackEdit stores your files in your browser, which means all your files are automatically saved locally and are accessible offline!
Adds one or more items to the end of an array. If called, returns the new length of the array. Modifies the existing array.
Removes the last item of an array. If called, returns the removed item. Modifies the existing array.
Adds one or more items to the beginning of an array. If called, returns the new length of the array. Modifies the existing array
Removes the first item of an array. If called, returns the the removed item. Modifies the existing array.
Calls a specified function on each item in the array. Returns a new array with new elements that have been created.
Calls specified function on each item in the array. It DOESN'T return anything.
Filters the array based on a given condition. Returns results in a new array.
Sorts the array based on given criteria.
Tests whether all items / every item in the array pass the test given by provided function you pass. Returns a boolean value.
Checks whether at least one item in the given array passes the test implemented by the passed in function. Returns boolean value (returns false if it's called on an empty array).
Changes the contents of the array by inserting or replacing items in the array
Creates a shallow copy of the array its being called on. Extracts up to but not including the end. Both beginning and end are optional.
Merges two arrays into a single array.
Checks whether the given value is present in the array. Returns true or false based on the findings.
Returns first index of item where it passes the test function, otherwise it returns -1.
Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex (optional argument, defaults to array.length - 1).
Joins the contents of an array into a string. Defaults to comma separated, if you provide separator, separator will be used instead.
Fills up an array with given number (first argument). Optional second and third argument that indicate start and end where the number is supposed to be filled in.
Returns value of first element in the array that passes the given test function.
Returns index of first element in array that passes the given test function.
Creates a new array with flattened structure up to given depth (optional argument).
Reverses the contents of an array. Modifies the existing array - destructible.
Creates a new, shallow-copied Array instance from an array-like or iterable object.
Creates an array of the given arguments. Numbers are treated as such, meaning array.of(7) will create an Array [7], not as the usual Array(7) which would create 7 empty slots.
Returns a new Array Iterator object that contains the key/value pairs for each index in the array.
returns a new Array Iterator object that contains the values for each index in the array.
returns new Array Iterator object that contains all the keys for each index in the array.
Returns a string of the elements in the array - comma separated.
The toLocaleString()
method returns a string representing the elements of the array. The elements are converted to Strings using their toLocaleString
methods and these Strings are separated by a locale-specific String (such as a comma “,”).
Shallow copies part of an array to another location in the same array and returns it without modifying its length.
Operates with a reducer function on an array. Can be used in lots of different use cases - can replace a map and filter and make it more concise. Takes up to four arguments. Needs an accumulator and currentValue argument. If accumulator isn't specified at start, we start with null. Current value is always the next item in the array. Accumulator is always the result of the previous iteration. Last accumulator will be returned.
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
Works similarly to array.reduce() but works the other way round. .We work from the last item in the array to the front of the array (right to left) instead of (left to right).
Returns boolean based on whether argument passed is an array or not. True if an array, false otherwise.