Array Knowledge Flashcards

1
Q

Shallow copy array, 4 ways

Why are they able to copy an array and what are they meant to do?

A

let newObj = oldObj.slice(first index, upper non inc index)

let newObj = oldObj .filter(function boolean goes to newObj)

let newObj = [ …oldObj]
spread syntax

let newObj = Array.from(string using the string letters as indexes or array, function return goes to new array with same indexes)

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

Create an array, 2 ways

A
let myArray = [ ]
let myArray = new Array(1, 2, ['nested array'])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Return array item

A

myArray[ ]

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

Return last element from array

A

myArray[myArray.length-1]

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

Create and return array properties

A

The same way as you would an object because arrays are objects.
These properties just don’t function with array index syntax and array methods that rely on indexes.

create
myArray[‘myKey’] = ‘my value’
or
myArray.myKey = ‘my value’

return with myArray.myKey or myArray[‘myKey’]

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

1.
Return THE INDEX AS A NUMBER of the first specific value in an array if it === with the argument
The second argument indicates which element to start looking from

2.
Can you do regex?

3.
What’s another one that returns the index but searches from the end of an array?

4.
What’s another one that returns the index but searches based on a callback function?

A

myArray.indexOf (searchelement, from index)
can’t do / /g

myArray.lastIndexOf(searchelement, from index )

myArray.findIndex(callback function)

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

Change an array element

A

myArray[old index] = ‘new element’

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

Add an array element to a specific index

A

myArray[new index] = ‘new element’

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

Return an array based off an array-like object argument .

The second argument can be a callback function. The function’s return is mapped onto the new array.

A

newArray = Array.from(myArray, callbackfunction)

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

Write function to compare arrays for shallow equality

A
function arraysEqual(arr1, arr2) {
  if (arr1.length !== arr2.length) return false;
  for (let i = 0; i < arr1.length; i += 1) {
    if (arr1[i] !== arr2[i]) {
      return false;
    }
  }

return true;
}

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

Why do array elements start at 0?

A

It’s how many elements away from the first element. Most programming languages behave this way.

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

What is returned from
let anArray = [1, 2, 3]
anArray[1,2]
?

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Having a comma at the end of an array or object is fine
let fruits = [
  "Apple",
  "Orange",
  "Plum",
];

t or f

A

T

AND it makes it easier to add more later a little bit

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

Add and call a function as an array element

A
let arr = [ 
  function() { 
    console.log('hello'); 
    console.log('goodbye');
  } ];
arr[0]()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Difference between queue and stack in terms of arrays.

A

Add to end, take from start.
Stack, LIFO (Last-In-First-Out) principle,

Add to end, take from end
Queue, FIFO(First in First out) principle

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

What is a deque?

A

A Double-ended queue.

Like arrays in JS

17
Q

What is another terms for things like Arrays in JS that allow adding and removing elements from either end?

A

deque

double ended queues

18
Q

What happens if you misuse an array?
The ways to misuse an array:
Add a non-numeric property like arr.test = 5.
Make holes, like: add arr[0] and then arr[1000] (and nothing between them).
Fill the array in the reverse order, like arr[1000], arr[999] and so on.

A

Regularly arrays try to store data contiguously (beside one another in memory).
They stop doing this if you use them like normal objects or have large gaps or fill them in reverse order.

19
Q

Why is it so much faster to work with the end of an array rather than the beginning?

A

Have to move all elements when adding or subtracting from the beginning of the array.
The longer the array the longer it will take