Arrays
array - is an ordered list of values that can be referred with an index
Constructor
new Array(...elements? / arrayLength?) - creates and initializes a new Array object
- const array = [] - more common and easy way of array creation. this is a shorthand notation for const array = new Array()
Array() - equals new Array(). it accepts same arguments
Static Methods
Array.from(iterable, callback?) - converts iterable object into array. returns array
- callback - mapping function. it will be run for each element of new array, in order to modify it
- equals to […iterable]
- tricks:
- Array.from({ length: 100 }, Math.random) - create array with random numbers. e.g. [0.68456085455823454, ...]
Array.isArray(value) - check if array
Methods
includes() - check if array includes element. returns boolean
- unlike indexOf() works with NaN
- tricks:
- conditionsArray.includes(false) - might be used inside if expression to check whether there is falsey condition within an array
indexOf() - get index of an element
- e.g. array.map((el, i) => el === foo ? i : null).filter((el) => el !== null) - find all indices containing specific duplicated element
lastIndexOf() - get index of an element when counting from backside of an array
slice(start?, end?) - returns part of an array. when called without parameters, copies and returns whole array
- start - index of the first element, which is included
- end - index of the first character, which is not included
concat(value, valueN?) - is used to merge two or more arrays. returns new array
- e.g. [1, 2, 3].concat(['x', 'y', 'z']) - returns [1, 2, 3, 'x', 'y', 'z']
join(connector?) - join array elements into a string
- returns string. for empty array returns empty string
- connector - default is comma
keys() - just returns array of indices
- tricks:
- […Array(100).keys()] - create array of unique elements, i.e. 100 elements with values 0 to 99
Mutating
mutating methods work in place. they modify original array
push(element, elementN?) - add element(s) to the end
- returns new array length
pop() - delete element from the end
- returns deleted element
unshift(element, elementN?) - add element(s) at the beginning
- returns new array length
shift() - delete element from the beginning
- returns deleted element
splice(start, deleteCount?, newElement?, newElementN?) - delete, add, or replace elements from any point of an array
- returns an array of deleted elements
- start - index at which to start. when start is negative, count begins from backside
- deleteCount - amount of elements to remove. for 0, first newElement is added right at start index
- toSpliced() - non-mutating alternative
reverse() - reverse an array
- toReversed() - non-mutating alternative
- reverse() mutates array and returns link to this array
fill(value, start?, end?) - change array elements to specific value
- end - not including
- fill() mutates array and returns link to this array
- e.g. [10, 11, 12].fill(5) - results in [5, 5, 5]
sort(callback?) - method for sorting. by default elements are compared as converted into string values, i.e. alphabetically. sorting order is ascending
- callback - is compare function, which is used for custom comparison logic
- callback accepts two elements. if positive number is returned - elements are swapped, if negative number or zero - elements are not swapped
- callback return value is being automatically converted to number
- sort() mutates array and returns link to this array. elements of resulting array do not change their data types
- e.g.
- sort((a, b) => a - b) - ascending sorting of numbers
- sort((a, b) => b - a) - descending sorting of numbers
- sort((a, b) => b.localeCompare(a)) - alphabetical descending sorting
- toSorted() - non-mutating alternative
Iterating
(element, index, array) => {...} - a testing function which is passed into each iterating method
- array - on each iteration this is a link to initial array
- callback for reduce() slightly differs, since reduce() passes one additional argument into the parameter
forEach(callback) - is used to iterate over an array. is alternative to for loop
- callback just executes. its return value is not considered
- method returns undefined
filter(callback) - is used to filter elements
- callback returns boolean value. if false -> value is omitted
- method returns resulting array. for empty array returns empty array
map(callback) - is used to modify each element
- callback returns updated value of an element
- method returns resulting array
findIndex(callback) - is used to find index of an element
- callback returns boolean value. true for an element being searched for. once found, iteration stops
- method returns index of the found element. returns -1, if element was not found
find(callback) - is used to find an element
- callback returns boolean. true for an element being searched for. once found, iteration stops
- method returns value of the found element. returns undefined, if element was not found
findLast() - alternative to find(), which finds last element
some(callback) - is used to check whether there is at least one element, which falls under some condition
- callback returns boolean value
- method returns boolean value. true if such element is present, false if not. returns false for empty array
every(callback) - is used to check whether all elements fall under some condition
- callback returns boolean value
- method returns boolean value. true if all elements fall, false if not. returns true for empty array
reduce(callback, initialValue?) - is used to iterate over an array and create custom resulting value
- (accumulator, element, index, array) => {...} - callback format for reduce()
- callback returns value, which is accumulator for the next callback call
- method returns last returned value from callback function. for empty array returns initialValue, if initialValue is missed - error
- initialValue - if not passed, callback call for the first element is omitted. accumulator for the second element is array's first element
- reduce() is handy for creating custom object. initialValue should be an object in such case
const recordsGroupedByDate = records.reduce((acc, record) => {
const recordGroup = acc[date] || [];
return {
...acc,
[record.date]: [
...recordGroup,
record,
],
};
}, {});flat()
flatMap()
iterating methods do not mutate original array