Array Accessors and Mutators Flashcards

1
Q

Access (index into) an Array item (index lookup)

A

array[0]

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

Add to the end of an Array

A

array.push()

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

Remove from the end of an Array

A

array.pop()

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

Add to the front of an Array

A

array.unshift()

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

Remove from the front of an Array

A

array.shift()

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

Find the index of an item in the Array

A

array.indexOf(‘item’)

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

Remove an item by index position

A
array.splice(pos, deleteCount, item) (mutable)
let array = [1,2,3]

array. splice(1, 0, 3) // [1,3,2,3]
array. splice(1, 1, 3) // [1,3,3]

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

Copy section of an Array

A
array.slice(start, end) (immutable - shallow copy)
let array = ['cat', 'dog', 'mouse']

array. slice(1) // [‘dog’, ‘mouse’]
array. slice(0,2) // [‘cat’, ‘dog’]

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

Convert the Array to a String

A

array.toString() [1, 2, ‘a’, ‘1a’] -> “1,2,a,1a”

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

Check for a specific value

A

array.includes()

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

Convert to separated String

A

array.join()
[‘Fire’, ‘Air’, ‘Water’] .join() -> “Fire,Air,Water”
.join(‘-‘) -> “Fire-Air-Water”

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

Populate all elements of an array to a static value

A

array.fill()

Array(3).fill({}) // [{}, {}, {}]

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

Merge two or more arrays into a new array (immutable)

A
array.concat() or spread [...array1, ...array2]
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
14
Q

Merge two arrays into an existing array (mutable)

A

existingArray.push.apply(existingArray, newArray)

let existingArray = [1,2,3]

let newArray = [4,5,6]

existingArray.push.apply(existingArray, newArray) // [1,2,3,4,5,6]

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

Combine nested arrays into single flattend array

A

array.flat() (immutable - returns new array)

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

Map over and array and then flatten

A

array.flatMap(() => {})

17
Q

Create an array of all of the indexes of an array

A

array.keys()