Array Methods Flashcards

1
Q

concat()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

indexOf()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

every()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

filter()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

findIndex()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

includes()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

map()

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

pop()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

push()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

reduce()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

shift()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

slice()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

some()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

sort()

A

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'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

splice()

A

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
17
Q

unshift()

A

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

const array1 = [1, 2, 3];

console.log(array1.unshift(4, 5));
// expected output: 5
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]