JS Array Flashcards
Array.concat(array)
Combines both arrays and returns a new array without changing existing
When do async tasks is it better to use a forEach method or a for loop?
The for loop is better. forEach() is synchronous.
forEach()
synchronous method that will be used on all elements in an array
every()
works on elements in an array until it returns a falsy value
some()
loops through the array until the callback returns a truthy value
filter()
creates a new array with the callback values that returned truthy
map()
creates a new array from the values returned by the callback
reduce()
reduceRight()
builds up a value by repeatedly using the callback
reduceRight is in descending order RTL
needs an accumulator ‘sum’ | ‘total’
const total = numbers.reduce((sum, value) => { return sum + value; })
push() and pop()
used to add to the tail and remove elements from the tail of an array
How is performance for forEach() or reduce() versus a traditional for loop?
forEach() and reduce() are faster for small to medium datasets, for loop is faster for large datasets (500,000+)
.from()
creates and array from a string
Array.from(“string”)
.toString()
returns a string with the array values seperated by commas
shift() and unshift()
Shift dequeues and element from the head
unshift inserts an element at the head
.sort() and .reverse()
sorts and reverses an array
.slice()
returns selected elements of an array as a new array(inclusive, exclusive)
.splice()
adds or removes items from an array
Array.splice(index, quantityToRemove, insert1, insert2,…)
includes()
returns true if an array contains the value
length
returns the size of the array
How can you instantiate an array of fixed size?
array = new Array(size);
Array.from()
Creates a new Array instance from an array-like object or iterable object.
Array.isArray()
Returns true if the argument is an array, or false otherwise.
Array.of()
The Array.of() method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.
Array.of(7); // [7] 1 slot
Array(7); // array of 7 empty slots
Array.at()
Returns the array item at the given index. Accepts negative integers, which count back from the last item.
Array.prototype.find()
Returns the value of the first element in the array that satisfies the provided testing function, orundefinedif no appropriate element is found.
Array.prototype.findLast()
Returns the value of the last element in the array that satisfies the provided testing function, orundefinedif no appropriate element is found.
Array.prototype.findIndex()
Array.prototype.findLastIndex()
Returns the index of the first element in the array that satisfies the provided testing function, or-1if no appropriate element was found.
Returns the index of the last element in the array that satisfies the provided testing function, or-1if no appropriate element was found.
Array.fill()
Fills all the elements of an array from a start index to an end index with a static value.
Array.prototype.join()
Joins all elements of an array into a string.
Array.prototype.keys()
Returns a newarray iteratorthat contains the keys for each index in the calling array.
Array.toLocaleString()
Returns a localized string representing the calling array and its elements. Overrides theObject.prototype.toLocaleString()method.
Array.prototype.indexOf()
Array.prototype.lastIndexOf()
Returns the first (least) index at which a given element can be found in the calling array.
Returns the last (greatest) index at which a given element can be found in the calling array, or-1if none is found.
Array.prototype.values()
Returns a newarray iteratorobject that contains the values for each index in the array.
Array.prototype.copyWithin()
Copies a sequence of array elements within an array.
Array.prototype.flat()
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Array.prototype.flatMap()
Returns a new array formed by applying a given callback function to each element of the calling array, and then flattening the result by one level.