Arrays Flashcards
Array.concat() ?
Parameters?
Returns?
used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
parameters: as many values (arrays) as you want
new Array instance
Array.every() ?
Parameters?
Returns?
tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
parameters: callbackFn - element, index (optional), array (optional)
bool
Array.at(index)
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.
returns element || undefined
arr = [1,2,3,4];
arr.length = 1;
What is length doing?
Truncating the array to one element
Array.length does what?
Returns the number of elements in the array (1 indexed)
Array.filter() ?
Parameters?
Returns?
creates a new array with all elements that pass the test implemented by the provided function
parameters: callbackFn that accepts - an element, index (optional), array (optional)
new filtered Array instance
Array.find() ?
Parameters?
Returns?
returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned
parameters: callbackFn that accepts - an element, index (optional), array (optional)
element || undefined
Array.findIndex() ?
Parameters?
Returns?
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
parameters: callbackFn that accepts - an element, index (optional), array (optional)
index of the first element in the array that passes the test || -1.
Array.flat() ?
Parameters?
Returns?
creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
parameters: depth
new array with the sub-array elements concatenated into it
Array.flatMap() ?
Parameters?
Returns?
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, but slightly more efficient than calling those two methods separately.
parameter: callbackFn which takes a current element, index, the array
new array with each element being the result of the callback function and flattened to a depth of 1
Array.forEach() ?
Parameters?
Returns?
executes a provided function once for each array element
parameters: callbackFn(element,index, the array)
undefined. doesn’t return anything just runs the function on each element.
Array.from() ?
Parameters?
Returns?
static method creates a new, shallow-copied Array instance from an array-like or iterable object.
parameters: An array-like or iterable object to convert to an array, (optional) map fn
new array instance
Array.includes() ?
Parameters?
Returns?
determines whether an array includes a certain value among its entries,
parameters: searchElement, (optional) fromIndex
* * method is case sensitive **
returns bool
Array.indexOf() ?
Parameters?
Returns?
returns the first index at which a given element can be found in the array
parameters: searchElement, (optional) fromIndex
returns first index of the element in the array or -1
Array.join() ?
Parameters?
Returns?
creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string
parameters: (optional) separator
returns A string with all array elements joined
Array.map() ?
Parameters?
Returns?
creates a new array populated with the results of calling a provided function on every element in the calling array
parameters: callbackFn(element, index, the array)
returns new array with each element being the result of the callback function
Array.pop() ?
Parameters?
Returns?
removes the last element from an array and returns that element. This method changes the length of the array.
parameters: none
returns the removed element from the array; undefined if the array is empty.
Array.push() ?
Parameters?
Returns?
adds one or more elements to the end of an array and returns the new length of the array
parameters: element(s)
returns updated array with new elements on the end.
Array.reduce()
Parameters?
Returns?
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.
callback fn: prevValue, curValue, curIndex, array, initial value (optional)
returns The value that results from running the “reducer” callback function to completion over the entire array.
Array.reverse()
Parameters ?
Returns ?
reverses an array in place. The first array element becomes the last, and the last array element becomes the first
params: none
returns the reversed array
Array.shift()
Parameters?
Returns?
removes the first element from an array and returns that removed element. This method changes the length of the array.
params: none
returns removed value || undefined if empty
Array.slice()
Parameters ?
Returns ?
returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
params: start (optional, can be negative) , end (optional, not included in the slice)
returns new shallow copy of the extracted array
Array.some()
Params?
Returns?
tests whether at least one element in the array passes the test. returns true if, it finds an element for which the provided function returns true; otherwise returns false. doesn’t modify the array.
callback fn: element, index, array
returns bool
Array.sort()
Params?
Returns?
sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
params: compare fn w/ 2 args (a,b) (optional)
returns an in-place sorted array
Array.splice()
Parameters?
Returns?
changes the contents of an array by removing or replacing existing elements and/or adding new elements in place
params: start, deleteCount, item(s)
returns an array of deleted elements
Array.toString()
Parameters?
Returns?
returns a string representing the specified array and its elements
params: none
returns string of array elements
Array.unshift()
Parameters?
Returns?
adds one or more elements to the beginning of an array and returns the new length of the array.
params: as many elements as you want
returns the new length property of the object upon which the method was called.