Array Methods Flashcards
Array()
Constructor method
Creates a new array object
Array.from()
Static Method
Creates a new array from an array-like instance or an iterable object
Array.isArray()
Static Method
Returns “true” if argument passed is an Array, “false” if it is not
Array.of()
Static Method
Creates a new array instance from the arguments, regardless of their quantity or data types
Array.prototype.length
Returns the number of elements in an array
Array.prototype.at()
Returns the array element at a given index
When passed a negative integer, it counts from the last element.
So [0, 1, 2, 3] .at ( -1 ) would return 3
Array.prototype.concat()
used to merge two or more arrays without mutating the input arrays
const array1 = ['a', 'b', 'c']; const array2 = ['d', 'e', 'f']; const array3 = array1.concat(array2)
//[“a”, “b”, “c”, “d”, “e”, “f”]
Array.prototype.every()
tests whether all elements in the input array pass the test implemented by the provided function
[0, 1, 2, 3].every((num) => num > -1)
Array.prototype.fill()
this method changes all elements in an array to a static value from a start index (default 0) to an end index (default array.length)
[1, 2, 3, 4, 5]. fill (0) —–> [0, 0, 0, 0, 0]
[1, 2, 3, 4, 5]. fill (0, 2, 4) ------> [1, 2, 0, 0] //fill array with zeros from idx 2 to idx 4 (not inclusive of idx 4)
[1, 2, 3, 4, 5]. fill (0, 1) —–> [1, 0, 0, 0, 0]
Array.prototype.filter()
The filter method creates a new array with all elements that pass the test implemented by the provided function
[0, 1, 2, 3]. filter( (num) => num < 2) —–> [0, 1]
Array.prototype.find()
The find method returns the first element in the array that satisfies the input function
[0, 1, 2, 3]. find( (num) => num === 3) //returns 3
Array.prototype.findIndex()
the findIndex method returns the index of the first element in the array that satisfies the provided testing function. Otherwise it returns -1, indicating that no element passed the test
[5, 12, 8, 130]. find ((num) => num > 12) //returns 3
Array.prototype.flat()
The flat ( ) method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
default depth = 1 (meaning a 1-dimensional arr)
Array.prototype.forEach()
The forEach method executes a provided function once for each element in an array
[‘a’, ‘b’, ‘c’] .forEach(element => console.log(element))
//a //b //c
Array.prototype.includes()
The includes() method determine whether an array contains a certain value
returns true or false