Array Methods Flashcards
Array.prototype@@iterator
??? Reexplain
The @@iterator method of Array instances implements the iterable protocol and allows arrays to be consumed by most syntaxes expecting iterables, such as the spread syntax and for…of loops. It returns an array iterator object that yields the value of each index in the array.
Array.prototype.at()
The at() method of Array instances 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.
const array1 = [5, 12, 8, 130, 44]; let index = 2; ${array1.at(index) // 8 index = -2; ${array1.at(index) // 130
Array.prototype.concat()
The concat() method of Array instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
const array1 = ['a', 'b', 'c']; const array2 = ['d', 'e', 'f']; const array3 = array1.concat(array2); console.log(array3); // Expected output: Array ["a", "b", "c", "d", "e", "f"]
Array.prototype.copyWithin()
The copyWithin() method of Array instances shallow copies part of this array to another location in the same array and returns this array without modifying its length.
copyWithin(target, start)
copyWithin(target, start, end)
const array1 = ['a', 'b', 'c', 'd', 'e']; // Copy to index 0 the element at index 3 console.log(array1.copyWithin(0, 3, 4)); // Expected output: Array ["d", "b", "c", "d", "e"] // Copy to index 1 all elements from index 3 to the end console.log(array1.copyWithin(1, 3)); // Expected output: Array ["d", "d", "e", "d", "e"]
Array.prototype.entries()
The entries() method of Array instances returns a new array iterator object that contains the key/value pairs for each index in the array.
const array1 = ['a', 'b', 'c']; const iterator1 = array1.entries(); console.log(iterator1.next().value); // [0, 'a'] console.log(iterator1.next().value); // [1, 'b'] for (const [index, element] of array1.entries()) { console.log(index, element); //0 'a', 1 'b', 2 'c', } for (const index of array1.entries()) { console.log(index); //[0, 'a'], [1, 'b'], [2, 'c'], } **FOR OBJECTS** const myObject = { 0: "a", 1: "b", 2: "d", }; Object.entries(myObject); //? for (const entry of Object.entries(myObject)) { console.log(entry); // [ '0', 'a' ], [ '1', 'b' ], [ '2', 'd' ] } let ent = Object.entries(myObject) ent[1][1] //[ [ '0', 'a' ], [ '1', 'b' ], [ '2', 'd' ] ]
Array.prototype.every()
The every() method of Array instances tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
const isBelowThreshold = (currentValue) => currentValue < 40; const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every(isBelowThreshold)); // Expected output: true const isBelowThreshold = (currentValue) => currentValue < 40; const array1 = [1, 30, 39, 29, 10, 50]; console.log(array1.every(isBelowThreshold)); // Expected output: false (50 is above)
Array.prototype.fill()
The fill() method of Array instances changes all elements within a range of indices in an array to a static value. It returns the modified array.
const array1 = [1, 2, 3, 4]; // Fill with 0 from position 2 until position 4 console.log(array1.fill(0, 2, 4)); // Expected output: Array [1, 2, 0, 0] // Fill with 5 from position 1 console.log(array1.fill(5, 1)); // Expected output: Array [1, 5, 5, 5] console.log(array1.fill(6)); // Expected output: Array [6, 6, 6, 6]
Array.prototype.filter()
The filter() method of Array instances 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.
const words = ['spray', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter((word) => word.length > 6); console.log(result); // Expected output: Array ["exuberant", "destruction", "present"]
Array.prototype.find()
The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
const array1 = [5, 12, 8, 130, 44]; const found = array1.find((element) => element > 10); console.log(found); // Expected output: 12
Array.prototype.findIndex()
The findIndex() method of Array instances 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.
const array1 = [5, 12, 8, 130, 44]; const isLargeNumber = (element) => element > 13; console.log(array1.findIndex(isLargeNumber)); // Expected output: 3
Array.prototype.findLast()
The findLast() method of Array instances 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.
const array1 = [5, 12, 50, 130, 44]; const found = array1.findLast((element) => element > 45); console.log(found); // Expected output: 130
Array.prototype.findLastIndex()
The findLastIndex() method of Array instances 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.
const array1 = [5, 12, 50, 130, 44]; const isLargeNumber = (element) => element > 45; console.log(array1.findLastIndex(isLargeNumber)); // Expected output: 3 // Index of element with value: 130
Array.prototype.flat()
The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
const arr1 = [0, 1, 2, [3, 4]]; console.log(arr1.flat()); // expected output: Array [0, 1, 2, 3, 4] const arr2 = [0, 1, [2, [3, [4, 5]]]]; console.log(arr2.flat()); // expected output: Array [0, 1, 2, Array [3, Array [4, 5]]] console.log(arr2.flat(2)); // expected output: Array [0, 1, 2, 3, Array [4, 5]] console.log(arr2.flat(Infinity)); // expected output: Array [0, 1, 2, 3, 4, 5]
Array.prototype.flatMap()
The flatMap() method of Array instances 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.
const arr1 = [1, 2, 1]; const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1)); console.log(result); // Expected output: Array [1, 2, 2, 1]
Array.prototype.forEach()
The forEach() method of Array instances executes a provided function once for each array element.
array1.forEach((element) => console.log(element)); // Expected output: "a" // Expected output: "b" // Expected output: "c"
Array.from()
The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object.
console.log(Array.from('foo')); // Expected output: Array ["f", "o", "o"] console.log(Array.from([1, 2, 3], (x) => x + x)); // Expected output: Array [2, 4, 6]
Array.fromAsync()
The Array.fromAsync() static method creates a new, shallow-copied Array instance from an async iterable, iterable, or array-like object.
Array.fromAsync(arrayLike)
Array.fromAsync(arrayLike, mapFn)
Array.fromAsync(arrayLike, mapFn, thisArg)
Parameters arrayLike An async iterable, iterable, or array-like object to convert to an array. mapFn Optional A function to call on every element of the array. If provided, every value to be added to the array is first passed through this function, and mapFn's return value is added to the array instead (after being awaited). The function is called with the following arguments: element The current element being processed in the array. Because all elements are first awaited, this value will never be a thenable. index The index of the current element being processed in the array. thisArg Optional Value to use as this when executing mapFn. Return value A new Promise whose fulfillment value is a new Array instance.