Array Methods Flashcards

1
Q

Array()

A

Constructor method

Creates a new array object

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

Array.from()

A

Static Method

Creates a new array from an array-like instance or an iterable object

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

Array.isArray()

A

Static Method

Returns “true” if argument passed is an Array, “false” if it is not

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

Array.of()

A

Static Method

Creates a new array instance from the arguments, regardless of their quantity or data types

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

Array.prototype.length

A

Returns the number of elements in an array

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

Array.prototype.at()

A

Returns the array element at a given index

When passed a negative integer, it counts from the last element.

So [0, 1, 2, 3] .at ( -1 ) would return 3

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

Array.prototype.concat()

A

used to merge two or more arrays without mutating the input arrays

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2)

//[“a”, “b”, “c”, “d”, “e”, “f”]

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

Array.prototype.every()

A

tests whether all elements in the input array pass the test implemented by the provided function

[0, 1, 2, 3].every((num) => num > -1)

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

Array.prototype.fill()

A

this method changes all elements in an array to a static value from a start index (default 0) to an end index (default array.length)

[1, 2, 3, 4, 5]. fill (0) —–> [0, 0, 0, 0, 0]

[1, 2, 3, 4, 5]. fill (0, 2, 4) ------> [1, 2, 0, 0]
//fill array with zeros from idx 2 to idx 4 (not inclusive of idx 4)

[1, 2, 3, 4, 5]. fill (0, 1) —–> [1, 0, 0, 0, 0]

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

Array.prototype.filter()

A

The filter method creates a new array with all elements that pass the test implemented by the provided function

[0, 1, 2, 3]. filter( (num) => num < 2) —–> [0, 1]

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

Array.prototype.find()

A

The find method returns the first element in the array that satisfies the input function

[0, 1, 2, 3]. find( (num) => num === 3)
//returns 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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

[5, 12, 8, 130]. find ((num) => num > 12)
//returns 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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.

default depth = 1 (meaning a 1-dimensional arr)

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

Array.prototype.forEach()

A

The forEach method executes a provided function once for each element in an array

[‘a’, ‘b’, ‘c’] .forEach(element => console.log(element))

//a
//b
//c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Array.prototype.includes()

A

The includes() method determine whether an array contains a certain value

returns true or false

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

Array.prototype.indexOf

A

Returns the first index at which a given element can be found in an array

returns -1 if element is not present

17
Q

Array.prototype.join()

A

creates an returns a new string by concatenating all of the elements in an array

items are separated by commas or a specified separator string

[0, 1, 2].join()
// '0,1,2'
[0,1,2].join('-')
// '0-1-2'
18
Q

Array.prototype.keys()

A

returns a new Array iterator object that contains the keys for each index in the input array

19
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 search backwards, starting at the fromIndex arg if one is passed

Array.lastIndexOf(‘word’, fromIndex)

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

console.log(animals.lastIndexOf('Dodo'));
// expected output: 3
console.log(animals.lastIndexOf('Dodo', 1));
// expected output: 0
20
Q

Array.prototype.map()

A

Returns a new array containing the results of invoking a function on each element of an input arry

[1, 4, 9, 16].map(x => x * 2)

// [2, 8, 18, 32]

21
Q

Array.prototype.pop()

A

pop() removes the last element in an array and returns that element

[0, 1, 2].pop()
//returns 2
//mutates input array to [0,1]
22
Q

Array.prototype.push()

A

push() adds one or more elements to the end of an array and returns its new length

[0, 0].push('cat')
//returns 3
//mutates input array to [0, 0, 'cat']
23
Q

Array.prototype.reduce()

A

The reduce method executes a user-supplied “reducer” callback function on each element of the array, passing the return value from the calculation on the preceding element. It returns a single value:

[0, 3, 6]. reduce ((prev, current) => prev + current) 
// 9
[0, 3, 6]. reduce ((prev, current) => prev * current)
//0