Array Accessors and Mutators Flashcards

(24 cards)

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 element to the front of an Array

A

array.unshift(element)

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, mutates original array.

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

Create an array of all of the indexes of an array

17
Q

Create and return a new string by concatenating all of the elements in this array,

A

array.join()

const elements = [“Fire”, “Air”, “Water”];
elements.join(“-“); // Expected output: “Fire-Air-Water”

18
Q

Returns the last index at which a given element can be found in the array,

A

const animals = [“Dodo”, “Tiger”, “Penguin”, “Dodo”];

animals.lastIndexOf(‘Dodo’) // 3

19
Q

Reverse an array in place, returns a reference to the original array

A

array.reverse()

20
Q

Removes the first element of an array and returns that element

A

array.shift()

21
Q

Sorts an array in place, returns a reference to the original array

22
Q

Returns an iterator with the values of given array

A

array.values()

23
Q

Replace a value with the given index. Returns a new array

24
Q

Returns a string representing the elements of an array using Locale

A

array.toLocaleString()

const array1 = [1, "a", new Date("21 Dec 1997 14:12:00 UTC")];
const localeString = array1.toLocaleString("en", { timeZone: "America/New_York" });