Array Flashcards
.push()
Adds x amount of elements to end of array & returns new length
* Mutating Method
The new length property of the object
Array.prototype.push(elementN)
.pop()
Removes last element and returns that element
* Mutating method
The removed element from the array; undefined if the array is empty
Array.prototype.pop()
shift()
Removes the first element and returns that removed element
* Mutating method
* Often used inside a while loop
The removed element from the array; undefined if the array is empty
Array.prototype.shift()
.unshift()
Adds x number elements to the beginning & returns new length
* Inserted in chunk at the beginning
The new length property of the object
Array.prototype.unshift(elementN)
slice()
Returns a shallow copy of a portion into a new array object
* end is not extracted, start is
* Often used with bind() and call() to create a utility method that converts an array-like object into an array
New array containing the extracted elements
Array.prototype.slice(start, end)
splice()
Changes the contents of an array by removing or replacing existing elements
* Mutating method
An array containing the deleted elements
Array.prototype.splice(start, deleteCount, itemN)
.sort()
Sorts the array in place
* Elements are converted to strings
* Should have a compareFn that specifies the sort order
The reference to the original array, now sorted
Array.prototype.sort((a,b) => {/*...*/})
.reverse()
Reverses the array in place
* Mutating method
* Preserves empty slots
The reference to the original array, now reversed
Array.prototype.reverse()
.concat()
Merge two or morre arrays
* Non-mutating method
* Creates a new array, first populated by elements in the object called on, then each argument is concatenated
A new Array instance
Array.prototype.concat(valueN)
.indexOf()
Returns the first index at which a given element can be found
* Uses strict equality
The first index of the element in the array; -1 if not found
Array.prototype.indexOf(searchElement, fromIndex)
.join()
Coverts the elements to a string and joins them together with a specified separator.
* if separator omitted, elements seperated by ,
* if array null / undefined, converted into an empty string
String with all elements joined. If length 0, the empty string returned
Array.prototype.join(separator)
.map()
New array with elements resulting from the function that was called onto each element.
* ^ can be an arrow function
* Iterative method & copying method
*
New array with each element being the result of the callback function
Array.prototype.map(callbackFn)
.filter()
Filters out elements on called array that pass the test implemented by the function
* Iterative method & copying method (shallow copy)
* Needs to return truthy to keep element in array
Shallow Copy, filtered to elements that pass the implemented test
Array.prototype.filter(testFunction(/*...*/))