JavaScript Array Methods Flashcards

1
Q

Array.from()

A

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

Array.isArray()

A

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

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

Array.of()

A

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]

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

Array.prototype.every()

A

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

Array.prototype.fill()

A

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

Array.prototype.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
7
Q

Array.prototype.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
8
Q

Array.prototype.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
9
Q

Array.prototype.concat()

A

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

Array.prototype.copyWithin()

A

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

Array.prototype.flat()

A

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

Array.prototype.forEach()

A

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

Array.prototype.includes()

A

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

Array.prototype.indexOf()

A

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

Array.prototype.join()

A

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

Array.prototype.lastIndexOf()

A

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

const animals = [‘Dodo’, ‘Tiger’, ‘Penguin’, ‘Dodo’];

console.log(animals.lastIndexOf('Dodo'));
// expected output: 3
console.log(animals.lastIndexOf('Tiger'));
// expected output: 1
17
Q

Array.prototype.map()

A

The map() method creates a new array 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]
18
Q

Array.prototype.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"]

plants.pop();

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]
19
Q

Array.prototype.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’];

const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push(‘chickens’, ‘cats’, ‘dogs’);
console.log(animals);
// expected output: Array [“pigs”, “goats”, “sheep”, “cows”, “chickens”, “cats”, “dogs”]

20
Q

Array.prototype.reduce()

A

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
21
Q

Array.prototype.toLocaleString()

A

The toLocaleString() method returns a string representing the elements of the array. The elements are converted to Strings using their to Loacle String mehtods and these Strings are separated by a locale-specific String (such as a comma “,”)

const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
const localeString = array1.toLocaleString('en', {timeZone: "UTC"});
console.log(localeString);
// expected output: "1,a,12/21/1997, 2:12:00 PM",
// This assumes "en" locale and UTC timezone - your results may vary
22
Q

Array.prototype.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”]

23
Q

Array.prototype.sort()

A

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.

const months = [‘March’, ‘Jan’, ‘Feb’, ‘Dec’];
months.sort();
console.log(months);
// expected output: Array [“Dec”, “Feb”, “Jan”, “March”]

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array[1, 100000, 21, 30, 4]

24
Q

Array.prototype.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
25
Q

Array.prototype.slice()

A

The slice() method returns a shallow copy of a portion of an array into a new array object seleceted from begin to end (end not included) where begin 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"]
26
Q

Array.prototype.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
27
Q

Array.prototype.reverse()

A

the reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// expected output: "array1:" Array ["one", "two", "three"]
const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]

/* Careful: reverse is destructive. It also changes
the original array */
console.log(‘array1:’, array1);
// expected output: “array1:” Array [“three”, “two”, “one”]

28
Q

Array.prototype.reduceRight()

A

the reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
(accumulator, currentValue) => accumulator.concat(currentValue)
);

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

The values() method returns a new Array Iterator object that contains the values for each index in the array.

A
const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
console.log(value);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"
30
Q

Array.prototype.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]
31
Q

Array.prototype.toString()

A

The toString() method returns a string representing the specified array and its elements.

const array1 = [1, 2, ‘a’, ‘1a’];

console.log(array1.toString());
// expected output: "1,2,a,1a"