JS array methods Flashcards
What are the three ways we can iterate over an array?
We can use 1.for loop 2.forEach() 3.for-of loop.
~~~
for (let i = 0; i < myArray.length; i++) { // Use myArray[i] to access each array element one by one } myArray.forEach(myElement => { // Use myElement to access each array element one by one }); for (const myElement of myArray) { // Use myElement to access each array element one by one } ~~~
What do push() and unshift() do?
push() adds an element at the end of an array. unshift() adds it at the beginning.
arr.push('item1', 'item 2'); //adds strings to the end of array arr.unshift(1, 2) //adds the values 1, 2, to the beginning of the array.
What do push() and unshift() methods return? and does it modify an existing array
Both also return, specifically, the new length of the array.both push() and unshift() modify existing arrays.
What are pop() and splice() used for?
both can be used methods to remove elements from an array.
What does pop() do?
pop() removes the last element of an array, and returns that element. This method changes the array.
let arr = [1,2,3,4] let popElement = arr.pop() //4 would be removed from array, and returned to be assigned
What does shift() do?
shift removes an item from the beginning of an array, and returns that element. This method changes the array.
let array = [1,2,3,4] let shiftElement = arr.shift() //1 would be removed and returned to be assigned
Break down the slice() method
slice() returns a shallow copy (does not change the orginal array)
the parameters are (start, end) representing index of items in that array.
note: start is inclusive, end is exclusive(not included) syntax: slice() //returns copy of whole array slice(start, end) //uses indexes provided to slice a specific portion slice(start) //slice will return an array from start index to array's end.
Break down the splice() method.
Ulitmately, splice can: insert, remove, and replace. Splice modifys arrays.
syntax: splice(start) //starting index splice(start, deleteCount) //number of elements to remove from start splice(start, deleteCount, item1, item 2, itemN) //elements to add to the array, beginning from start.
Break down the slice() method.
slice returns a shallow copy of an array based off arguements
syntax: slice() //returns a copy of entire array slice(start) //returns copy of array, using (inclusive) starting index to end slice(start, end) //returns copy using (inclusive) start, to (exclusive) end.
What does concat() method do?
Concat creates a new array that includes values from other array and additional items.
notes: Any number of arguement - can be either arrays or values. syntax: concat(arg1, arg2)
Breakdown forEach()
forEach() allows a function to run for every element of an array
syntax: forEach(function (item, index, array) ){ //..function to run on elements}
Explain indexOf() and lastIndexOf()
indexOf() looks for -item- starting from index -from-
and returns the index where it was found, otherwise returns 0
syntax: indexOf(item, from) lastIndexOf() is the same, but works backwards in an array
What does include() do?
Checks to see if an item exists in an array. returns true / false.
syntax: include(item, from) //starts searching at -from- index
explain find() method
find() returns the first element in the provided array that satisfies the provided testing function.
if no values satisfy -undefined- is returned.
syntax: find( (element)=>{}) //current element being processed in the array find( (element, index)=>{}) //index of the current element being processed in the array find( (element, index, array)=>{}) //the array find() was called upon find(callbackFn) //a function to execute for each element in the array. it should return a truthy value to indicate a matching element has been found find(callbackFn, thisArg) //a value to use -this- when executing callbackFn (see iterative methods)
How to determine use cases for findIndex(), indexOf(), include(), some(), etc
findIndex() : if you need the INDEX of the found element in array
indexOf() : if you need to find the INDEX of a VALUE (it checks each element for equality instead of test function)
includes() : if you need to check if a value EXISTS in array. (checks with equality instead of function)
some() : if you need to find if any elements satisfy provided testing function.