Array Knowledge Flashcards
Shallow copy array, 4 ways
Why are they able to copy an array and what are they meant to do?
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)
Create an array, 2 ways
let myArray = [ ] let myArray = new Array(1, 2, ['nested array'])
Return array item
myArray[ ]
Return last element from array
myArray[myArray.length-1]
Create and return array properties
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’]
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?
myArray.indexOf (searchelement, from index)
can’t do / /g
myArray.lastIndexOf(searchelement, from index )
myArray.findIndex(callback function)
Change an array element
myArray[old index] = ‘new element’
Add an array element to a specific index
myArray[new index] = ‘new element’
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.
newArray = Array.from(myArray, callbackfunction)
Write function to compare arrays for shallow equality
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;
}
Why do array elements start at 0?
It’s how many elements away from the first element. Most programming languages behave this way.
What is returned from
let anArray = [1, 2, 3]
anArray[1,2]
?
3
Having a comma at the end of an array or object is fine let fruits = [ "Apple", "Orange", "Plum", ];
t or f
T
AND it makes it easier to add more later a little bit
Add and call a function as an array element
let arr = [ function() { console.log('hello'); console.log('goodbye'); } ]; arr[0]()
Difference between queue and stack in terms of arrays.
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