Array Methods Flashcards
Array.from()
creates a new, shallow-copied Array instance from an array-like or iterable object.
> Array.from((string,array),(map func))
Array.of()
creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.
> Array.of(7) // [7]
> Array.of(1,2,3) // [1, 2, 3]
Array()
array constructor, if passed in an integer, creates that many empty spots for the integer, or if a list of integers, creates that array
> Array(7) // creates an array with 7 empty spots of that length
> Array(1, 2, 3) // [1, 2, 3]
.concat()
Merges two arrays together, doesn’t change the existing arrays, returns new array
> array1 = [ 1, 2, 3], array2 = [4, 5, 6]
> array1.concat(array2) // [1, 2, 3, 4, 5, 6]
.every()
tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
> .every((function_to_test)) // True or false
.fill()
changes all elements in an array to a static value
.fill((value),(start),(end))
.filter()
creates a new array based on if the elements pass the given function
.find()
returns the value of the first element in the provided array that satisfies the provided testing function.
.findIndex()
returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.
.includes()
determines whether an array includes a certain value among its entries, returning true or false as appropriate.
.pop()
removes the last element from an array and returns that element. This method changes the length of the array.
.reverse()
reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
.shift()
removes the first element from an array and returns that removed element. This method changes the length of the array.
.some()
tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
> array.some((func)) // true or false
.unshift()
adds one or more elements to the beginning of an array and returns the new length of the array.
> array.unshift((element1),(element2)…)