fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.
From constructor.
The Array.from() static method creates a new, shallow-copied Array instance from an array-like or iterable object.
Methods
.length: Array’s elements count.
Adding items
.push: Appends an item to the array.
.unshift: Add an item to the array at the beginning.
Removing Items
.shift: Removes an item from the array’s beginning.
.pop: Removes an item from the array’s end.
Removing an Item by its index
Finding
indexOf: returns the first index at which a given element can be found in the array, or -1 if it is not present.
includes(): determines whether an array includes a certain value among its entries, returning true or false as appropriate.
Slice
Extract a part of an array without changing the original array. Similar to slice in strings.
Splice
Similar to slice, but changes the array content directly.
Reverse
Reverses an array, changes the original array.
Concat
Merges two arrays.
Join
Joins an array items into a string.
At
at() method takes an integer value and returns the item at that index, allowing for positive and negative integers.
Getting last array element
Data Transformation
Map
Similar to the forEach method, but returns a new array containing the results of applying an operationon all original array elements.
Filter
Returns a new array containing the array elements that passed a specified test condition. It can also access the current item index and whole array second and third parameters.
Reduce
Boils (“reduces”) all array elements down to one single value (e.g. adding all elements together) (Snowball). It returns only a reduced value.
Chaining Data Transformation Methods
Many array methods can be chained to perform powerful data transformations.
reduce doesn’t return an array but a value instead, and thus it can’t be chained to another method.
Find
Can be used to retrieve one array’s element based on a certain condition.
Unlike filter, it won’t return a new array, but the first element that matches the finding condition. Because of this, it can be more efficient in situations when something needs to be done in case of a match.
findIndex
Similar to find, but returns the found element index and not the element itself.
Some and Every
Similar to includes, but check for a condition instead of equality.
Some: returns true if the condition is true for any element.
Every: returns true if the condition is true for all elements.
Checking condition can be written as a separate function, then passed as callback to every/some methods.
Flat and flatMap
Introduced in ES2019.
flat creates a new array with all sub-array elements concatenated into it recursively up to the specified depth (defaults to 1).
flat method removes empty slots in arrays.
flatMap returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. Equal to arr.map(...args).flat().
Note that with flatMap only one level is flattened, so if there’s a need for flattening deeper map then flat must be used.
Sort
sort method sorts items by alphabetical order.
It changes the original array. This can be avoided by calling slice on the original array before sorting.
Numbers are being treated as strings.
To deal with numbers properly we can use a callback function that takes two parameters: current and next, and should return:
< 0 to sort current before next. (return something less than zero to keep order)
> 0 to sort current after next. (return something higher than zero to switch order)
- to keep original order of current and next.
To compare numbers instead of strings, the compare function can subtract b from a. The following function will sort the array in ascending order (if it doesn’t contain Infinity and NaN).
Summary: Which Array Method to Use?
To mutate original array
Add to original
.push (end)
.unshift (start)
Remove from original
.pop (end)
.shift (start)
.splice (any)
Others
.reverse
.sort
.fill
A new array
Computed from original
.map (loop)
Filtered using condition
.filter
Portion of original
.slice
Adding original to other
.concat
Flattening the original
.flat
.flatMap
An array index
Based on value
.indexOf
Based on test condition
.findIndex
An array element
Based on test condition
.find
Know if array includes
Based on value
.includes
Based on test condition
.some
.every
To transform to value
Based on accumulator
.reduce (Boil down array to single value of any type: number, string, boolean, or even new array or object)
A new string
Based on separator string
.join
To just loop array
Based on callback
.forEach (Does not create a new array, just loops over it)