Array Methods Flashcards
concat()
This method joins several arrays into one.
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"]
indexOf()
This method returns the first position at which a given element appears in an array
const beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console.log(beasts.indexOf('bison')); // expected output: 1
every()
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
function isBigEnough(element, index, array) { return element >= 10; }
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
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"]
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
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
includes()
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
const pets = [‘cat’, ‘dog’, ‘bat’];
console.log(pets.includes('cat')); // expected output: true
map()
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
const array1 = [1, 4, 9, 16];
// pass a function to map const map1 = array1.map(x => x * 2);
console.log(map1); // expected output: Array [2, 8, 18, 32]
pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
const plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];
console.log(plants.pop()); // expected output: "tomato"
console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
const animals = ['pigs', 'goats', 'sheep']; animals.push('cows')
console.log(animals); // expected output: Array ["pigs", "goats", "sheep", "cows"]
reduce()
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.
const arr = [1, 2, 3, 4];
// 5 + 1 + 2 + 3 + 4 console.log(arr.reduce((accumulator, currentVal) => accumulator + currentVal, 5)); // expected output: 15
shift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1); // expected output: Array [2, 3]
console.log(firstElement); // expected output: 1
slice()
The slice() method 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.
const animals = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘elephant’];
console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"]
console.log(animals) // expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
some()
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
const array = [1, 2, 3, 4, 5];
// checks whether an element is even const even = (element) => element % 2 === 0;
console.log(array.some(even)); // expected output: true
sort()
The sort() method sorts the elements of an array in place and returns the sorted array.
var mixedNumericArray = [‘80’, ‘9’, ‘700’, 40, 1, 5, 200];
function compareNumbers(a, b) { return a - b; }
console.log('Sorted with compareNumbers:', mixedNumericArray.sort(compareNumbers)); // expected output: 'Sorted with compareNumbers: 1,5,9,40,80,200,700'