JavaScript Array Methods Flashcards
Array.from()
A method that creates a new, shallow-copied Array instance from an array-like or iterable object:
console.log(Array.from('foo')); // expected output: Array ["f", "o", "o"]
Array.isArray()
A method that determines whether the passed value is an Array:
Array.isArray([1, 2, 3]); // true
Array.isArray({foo: 123}); // false
Array.isArray(‘foobar’); // false
Array.isArray(undefined); // false
Array.of()
A method that 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(7); // array of 7 empty slots
Array(1, 2, 3); // [1, 2, 3]
Array.prototype.every()
The every() method that tests whether all elements in the arras 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
Array.prototype.fill()
The fill() method fills (modifies) all the elements of an array from a start index (default 0) to an end index (default array length) with a static value
const array1 = [1, 2, 3, 4];
// fill with 0 from position 2 until position 4 console.log(array1.fill(0, 2, 4)); // expected output: [1, 2, 0, 0]
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
const words = [‘spray’, ‘limit’, ‘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 returns the value of the first element in the provided array that satisfies the provided testing function.
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 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.
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber)); // expected output: 3
Array.prototype.concat()
The concat() method 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'];
console.log(array1.concat(array2)); // expected output: Array ["a", "b", "c", "d", "e", "f"]
Array.prototype.copyWithin()
The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length.
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.flat()
The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth
var arr5 = [1, 2, , 4, 5]; arr5.flat(); // [1, 2, 4, 5]
Array.prototype.forEach()
The forEach() method executes a provided function once for each array element.
const array1 = [‘a’, ‘b’, ‘c’];
array1.forEach(element => console.log(element));
// expected output: "a" // expected output: "b" // expected output: "c"
Array.prototype.includes()
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
const array1 = [1, 2, 3];
console.log(array1.includes(2)); // expected output: true
const pets = [‘cat’, ‘dog’, ‘bat’];
console.log(pets.includes('cat')); // expected output: true
console.log(pets.includes('at')); // expected output: false
Array.prototype.indexOf()
The indexOf() method returns the first index at which a given element can be fond in the array, or -1 if it is not present.
const beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console.log(beasts.indexOf('bison')); // expected output: 1
// start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: 4
console.log(beasts.indexOf('giraffe')); // expected output: -1
Array.prototype.join()
The join() method 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. If the array has only one item, then that item will be returned without using the separator.
const elements = [‘Fire’, ‘Air’, ‘Water’];
console.log(elements.join()); // expected output: "Fire,Air,Water"
console.log(elements.join('')); // expected output: "FireAirWater"
console.log(elements.join('-')); // expected output: "Fire-Air-Water"