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