Arrays Flashcards

1
Q

How do we create an array?

A

let arrayName = []

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

What can we store in an array?

A
  • Any valid data type
  • objects
  • arrays
  • functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we find the value of an item in an array?

A

we query the array with arrayName[n]

n is the index of the item in the array

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

What happens if we query an array with an index larger than the array holds

A

we get ‘undefined’ as the result

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

Can we get items from the end of an array?

A

Yes, by using negative indices we can query from the end of an array.

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

How could target the end of an array?

A

arrayName[arrayName.length - 1]

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

How can we assign a value to an item in an array

A

arrayName[n] = someValue

will create or reassign the value of an item in an array

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

if you insert a item value deep into an array, what will the intervening values be set to?

A

undefined

(or empty in google chrome)

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

What method could we use to find the index of a value in an array?

A

We could use the .indexOf method to find the index of a value.

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

Describe the indexOf method syntax on an array

A

arrayName.indexOf(value)

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

What data types will indexOf work on?

A

indexOf works on primitive data types

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

If a value is not present in an array, what does indexOf return

A

indexOf will return -1 if a value does not exist in an array.

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

How can we add a value to the front of an array?

A

we can use the unshift method to add a value to the front of an array.

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

How can we add a value to the end of an array?

A

We can use the push method to add a value to the end of an array.

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

What are two methods used for adding values to an array

A
  1. .unshift - to the front of an array
  2. .push - to the end of an array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can we remove a value from the start of an array?

A

We can use the .shift method to remove a value from the start of an array

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

What is the syntax for using the shift method on an array

A

arrayName.shift()

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

What method can we use to remove a value from the end of an array

A

We can use the pop method to remove a value from the end of an array

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

Name two methods we can use to remove values from an array

A
  1. .shift - remove from front of array
  2. .pop - remove from end of array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does the splice method allow us to do?

A

It allows us to add or remove values from any position in an array

21
Q

What is the splice method syntax on an array?

A

arrayName.splice(index, deleteCount, itemsToAdd)

22
Q

When using the splice method, where does the editing begin?

A

It begins with the index value given as the first argument in the method.

23
Q

If we want to only add, and not delete, items using the splice method, how do we do so?

A

We set the the deleteCount argument, the second argument to the splice method, to zero.

.splice(index, 0, itemsToAdd)

24
Q

If we are removing items from an array using splice, which index will it remove first

A

splice will remove the length starting with the index passed as the first argument.

25
Q

If we are adding items to an array using splice, which value will be the first to change.

A

the splice method begins editing at the index given as its first argument, so:

  • splice(0,0,1) makes index 0 = 1
  • splice(3,0,2) makes index 3 = 2
26
Q

If we are adding items to an array using splice, which value will be the first to change.

A

the splice method begins editing at the index given as its first argument, so:

  • splice(0,0,1) makes index 0 = 1
  • splice(3,0,2) makes index 3 = 2
27
Q

How do we use the slice method on an array?

A

The slice method copies a section of an array, without changing the original

We generally save this new array to a variable

28
Q

How do the splice and slice methods on arrays differ

A

Splice modifies the original array

Slice copies a slice of the original, but does not modify the original array

29
Q

Give the syntax for the slice method on arrays

A

arrayName.slice(start, end)

start = includes start index

end = excludes end index

30
Q

Given:

array = [1, 2, 3, 4, 5, 6]

newarray = array.slice(1, 4)

What does newarray contain?

A

newarray = [2, 3, 4]

31
Q

Do we need to pass parameters to the shift method.

A

No, the shift method remove only the first element in an Array

32
Q

If an array is mutated in a function, does the mutation persist outside the function scope?

A

Yes, if the mutation was destructive, then the mutation persists.

33
Q

What do we mean by passed-by-reference?

A

passed-by-reference refers to the fact that when we pass an array into a function, we are passing a reference to that array in memory.

34
Q

How do we use the .find method in JavaScript?

A

The .find method is used to check an array of objects for the presence of an object with a specific condition.

35
Q

How many arguments are typically used in the .find method?

A

We typically only use one argument in the .find method, the item argument (the others are index, array

36
Q

What is the syntax for using a .find method on an array of objects in JavaScript?

A

array.find(item => item.id == 1);

This would return the object with the presence of id: 1

If we saved the result to a variable, we could then access other key:value pairs within the object

let user = users.find(item => item.id == 1)

console.log(user.name) - would log the value of the name key from the object with id: 1

37
Q

When does the method .find stop iterating over an array?

A

.find stops iterating over an array when the first truthy value is found.

If nothing is found it returns undefined.

38
Q

What method should we use on an array if we want all of a certain value returned?

A

We should use the .filter method on an array to return all the occurrences of a certain value.

39
Q

What is the syntax of a .filter method call?

A

let results = array.filter(item => item > 10)

results would then be an array with any value > 10

40
Q

Can we use the .filter method on an array of objects?

A

Yes, .filter can be used on an array of objects to create an array of all objects matching certain criteria.

41
Q

Give the syntax of using the .filter method on an array of objects.

A

let results = arr.filter(item => item.id < 3)

for(let result of results){

console.log(result.name)

}

42
Q

How is the .sort method used on arrays?

A

The .sort method with sort an array alphabetically (even if the contents are intergers), unless we provide a function as an argument to .sort

43
Q

What is a typical .sort syntax used to sort items numerically?

A

arr.sort((a,b) => a - b)

when a - b produces a positive result the order needs to be reversed (i.e. a > b)

for this reason, to sort in reverse we use b - a, when this produces a positive result the order is kept (i.e. b > a)

44
Q

What method might we pass to the function inside the .sort method to sort strings alphabetically?

A

We would use the localeCompare method because, unlike sort a - b, it compares the letters alphabetically instead of based on their character code value.

arr.sort((a,b) => a.localeCompare(b)

45
Q

Instead of sorting using b - a in our .sort function, what might we do to reverse an array?

A

We could use the .reverse method to sort from higher to lower (in descending order)

46
Q

Why is it good practice to include the initial value when using the .reduce method?

A

It is good practice, because if the array is empty the .reduce method will throw an error.

47
Q

What is the syntax of a reduce method call?

A

let arrSum = arr.reduce((sum, current) => sum + current, 0)

Where the 0 is the initial value to start with.

48
Q

What does the method .reduceRight do for us in JavaScript?

A

.reduceRight begins it’s iteration from the right side of the array

49
Q

What does the syntax Array.isArray(x) do for us in JavaScript and why would we use it?

A

Array.isArray(x) checks to see if x is an array, and returns true if it is.

We use it to determine if a data structure is an array or not, as in JavaScript, arrays are object-based data-structures.