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
- 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 on array of conditions
indexOf() - get index of an element
- tricks:
- array.map((el, index) => el === foo ? index : null).filter((el) => el !== null) - find all indexes for 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. for negative "start", count begins from backside
- deleteCount - amount of elements to remove. for 0, first new element is added right at "start" index
- toSpliced() - non-mutating alternative
reverse() - reverse an array
- toReversed() - non-mutating alternative
fill(value, start?, end?) - change elements to static value
- end - not including
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 updated 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
testing function - a special callback which is passed into each iterating method
- (element, index, array) => {...} - format of callback for all methods except "reduce". "reduce" passes one additional argument into callback
- array - on each iteration this is a link to initial array
forEach(callback) - is used to iterate over an array. is analog of "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 type 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 - initial value. 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