Array Methods Flashcards
How would I obtain the length of an array?
array.length
const myArray = [1, 2, 3, 4]
console.log(myArray.length) // prints 4
How would I add one or more elements to the end of an array?
array.push()
const myArray = [1, 2, 3, 4]
console.log(myArray.push(5)) // prints [1, 2, 3, 4, 5]
Returns the new length of the array.
How would I remove and return the last element from an array?
array.pop()
~~~
const myArray = [1, 2, 3, 4]
const lastElement = myArray.pop()
~~~
console.log(myArray)
// prints [1, 2, 3]console.log(lastElement)
// prints 4
Mutates the original array.
How would I remove and return the first element of an array?
array.shift()
~~~
const myArray = [1, 2, 3, 4];
const firstElement = myArray.shift();
~~~
console.log(myArray);
// prints [2, 3, 4]console.log(firstElement);
// prints 1
Mutates the original array.
How would I add one or more elements to the beginning of an array?
array.unshift()
~~~
const myArray = [1, 2, 3, 4];
myArray.unshift(0);
console.log(myArray);
``` // prints [0, 1, 2, 3, 4]
How would I return a portion of an array as a new array?
array.slice(a, b)
Parameter a is starting index.
Parameter b is ending ending index, which is not included.
const myArray = [1, 2, 3, 4]; const newArray = myArray.slice(1, 3)
console.log(newArray)
// prints [2, 3];console.log(myArray)
// prints [1, 2, 3, 4];
The original array is not mutated.
How would I remove and/or replace existing elements at any location in an array?
array.splice(a, b, c)
Parameter a is starting index (required)
Parameter b is number of elements to remove (required)
Paremeter c is one or more elements to be inserted (optional)
~~~
const myArray = [1, 2, 3, 4];
my array.splice(2, 1, “a”, “b”);
console.log(myArray)
``` // prints [1, 2, “a”, “b”, 4]
How would I combine two or more arrays?
Method 1:
array.concat()
~~~
const array1 = [1, 2]
const array2 = [3, 4]
const array3 = [5, 6]
const combinedArray = array1.concat(array2, array3)
~~~
Method 2:
Spread operator
const combinedArray = [...array1, ...array2, ...array3]
How would I check whether all elements in an array pass a certain condition?
array.every(function)
~~~
const numbers = [2, 4, 6, 8];
const areAllEven = numbers.every(num => num % 2 === 0);
console.log(areAllEven);
``` // true
How would I create a new array with all elements that pass a provided test?
array.filter(function)
~~~
function isBigEnough(value) {
return value >= 10;
}
const filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
``` // filtered is [12, 130, 44]
How would I return the value of the first element in an array that satisfies a provided test?
array.find(function)
~~~
const numbers = [2, 4, 6, 8, 10];
const foundNumber = numbers.find(function (num) {
return num > 5;
})
console.log(foundNumber)
``` // 6
Returns undefined if no element satisfies condition.
How would I return the index of the first element in an array that satisfies a provided test?
array.findIndex(function)
~~~
const numbers = [2, 4, 6, 8, 10];
const firstEvenIndex = numbers.findIndex(num => num % 2 === 0);
console.log(firstEvenIndex)
``` // 0
Returns -1 if no elements pass the test.
How would I create a new array with sub-array elements flatted by a specified depth?
array.flat()
~~~
const arr = [1, 2, [3, 4]];
const flattened = arr.flat();
console.log(flattened);
``` // [1, 2, 3, 4]
How would I loop through an array and perform a function on each element using the .forEach() method?
array.forEach(element, index, array)
element parameter is current value of element being processed
index parameter is index of current element being processed
array parameter is array that forEach() is being called on
Example : Looping through array and logging each element
const myArray = ['apple', 'banana', 'orange']; myArray.forEach((element) => { console.log(element); //apple banana orange });
Example 2: Modifying the elements of an array
~~~
const myArray = [1, 2, 3];
myArray.forEach((element, index, array) => {
array[index] = element * 2;
});
console.log(myArray);
``` //[2, 4, 6]
How would I check whether an array contains a certain value?
array.includes(value)const arr = ["apple", "banana", "orange"];
console.log(arr.includes("banana"));
// trueconsole.log(arr.includes("BANANA"));
// falseconsole.log(arr.includes("grape"));
// false
Returns boolean value.