Array methods Flashcards
Array.prototype.at()
Takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
[1, 2, 3].at(1) // will return 2
Array.prototype.concat()
Merge two or more arrays. Does not change the existing arrays, but instead returns a new array.
let newArr = [1,2,3].concat([4,5,6]) // will return [1,2,3,4,5,6]
Array.prototype.copyWithin()
Shallow copies part of this array to another location in the same array and returns this array without modifying its length.
Parameters - target (where to start copying to), start (where to start copying from), end (optional - where to stop copying from, if omited, copy to end of array up to length of the array)
[1,2,3,4,5].copyWithin(1, 3) // will return [1, 4, 5, 4, 5]
Array.prototype.every()
Tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
[1,2,3,4,5].every(item => item < 10) // returns true
Array.prototype.fill()
Changes all elements within a range of indices in an array to a static value. It returns the modified array.
Parameters - value (what to fill array with), start (optional - where to start filling), end (optional - where to end filling, up to and not including end, if absent fill to end)
[1,2,3,4,5].fill(1, 5) // returns [1, 5, 5, 5, 5]
Array.prototype.filter()
Creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function
[1, 2, 3, 4, 5].filter(item => item % 2 == 0) // returns [2, 4]
Array.prototype.find()
Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
[5, 8, 12, 25, 114].find(item => item > 10) // returns 12
Array.prototype.findIndex()
Returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
[5, 8, 12, 56, 145].findIndex(item => item > 13) // returns 3
Array.prototype.findLast()
Iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned.
e.g.
[5, 8, 50, 130, 12].findLast(item => item > 30) //returns 130
Array.prototype.findLastIndex()
Iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
[5, 8, 130, 48, 12].findLastIndex(item => item > 30) // returns 3
Array.prototype.flat()
Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Parameters - depth (optional - the depth level of the flattening, defaults to 1)
[1,2,[3,4]].flat() // returns [1,2,3,4]
Array.prototype.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. It is identical to a map() followed by a flat() of depth 1 (arr.map(…args).flat()), but slightly more efficient than calling those two methods separately.
[1, 2, 1}.flatMap((num) => (num === 2 ? [2, 2] : 1)) // returns [1, 2, 2, 1]
Array.prototype.forEach()
Executes a provided function once for each array element. Return value is undefined.
~~~
[1, 2, 3].forEach(item => console.log(item)) // logs each item to the console
Array.prototype.includes()
Determines whether an array includes a certain value among its entries, returning true or false as appropriate.
[5, 10, 15].includes(10) // returns true
Array.prototype.indexOf()
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
[5, 10, 15, 20].indexOf(15) // returns 2
Array.prototype.join()
Creates and returns a new string by concatenating all of the elements in this array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
['c', 'a', 't'].join("") // returns 'cat'
*
Array.prototype.keys()
Returns a new array iterator object that contains the keys for each index in the array.
Array.prototype.lastIndexOf()
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.
Parameters - search element (item searched for), fromindex (optional - index to start searching from)
[1, 2, 3, 1].lastIndexOf(1) // returns 3
Array.prototype.map()
Creates a new array populated with the results of calling a provided function on every element in the calling array.
[1, 2, 3].map(item => item * 2) // returns [2, 4, 6]
Array.prototype.pop()
Removes the last element from an array and returns that element. This method changes the length of the array.
[1, 2, 3, 4].pop() // returns 4
Array.prototype.push()
Adds the specified elements to the end of an array and returns the new length of the array.
['apple', 'bears', 'cows'].push('dust') // returns 4
Array.prototype.reduce()
Executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
[1, 2, 3, 4].reduce((acc, curr) => acc + curr) // returns 10
Array.prototype.reverse()
Reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.
~~~
[1, 2, 3].reverse() // returns [3, 2, 1]
Array.prototype.shift()
Removes the first element from an array and returns that removed element. This method changes the length of the array.
[1, 2, 3, 4].shift() // returns 1