JS Array Methods Flashcards
Practice remembering what specific array methods can do, and what situation it may be ideal to use over another.
What does
~~~
pop()
~~~
do?
Removes the last element of an array, decrementing the array length, and returning the value that was removed. (This alters the original array)
no arguments
https://www.codecademy.com/resources/docs/javascript/arrays/pop
What doespush()
do?
Adds one or more elements to the end of an array and returns the new length. (Original array is altered)
arguement(s) is new elements (of any datatype) ex:
arr.push(item1, ‘item2’, …);
What does
~~~
shift()
~~~
do?
Removes and returns the first element of an array. All subsequent elements shift down one place.
no arguements needed. Alters original array.
What does
~~~
unshift()
~~~
do?
Adds one or more elements to the beginning of array and returns new length.
Alters original array. Arguements consist of new elements to insert. ex:
arr.unshift( item1, item 2, …);
What does
~~~
.slice()
~~~
do?
returns a shallow copy of all or parts of an array without modifying the original.
Creates new ‘shallow’ array. Arguements consist of (arguement index) up to but not including (2nd arguement index) ex:
arr.slice (0-5) //will copy index 0 to 4.
second arguement is not needed.
if no arguements, entire array is copied.
arguements can be negative (indicating offset from end of sequence) but 2nd arguement behavior still applies.
What does
.splice()
do?
swiss army method - it can insert, remove, and replace elements.
syntax: arr.splice(starting index, deleteCount, newInsertedElement(s),…)
What does
.slice()
do?
Returns a new array, copying elements from startIndex, and endIndex(not including end) arguements.
syntax: arr.slice(startIndex, endIndex)
What does
.concat()
do?
creates a new array, concatinating(putting together) the array it’s used on, with other array, array values, values, or objects.
syntax: arr.concat([3,4], 5, 6)
In order to use .concat() on an array-like Object, how can we have said object treated as an array by .concat()?
If the object has the property of :
\[Symbol.isConcatSpreadable]: true
it’s elements will be added with the behavior of normal arrays.
What is
.forEach()?
.forEach() method is used to iterate/run a function for every element of the array
syntax: arr.ForEach(function(element, index, array){}
What does
.indexOf()
do?
looks for item starting from indexStart, and returns the index where it was found. Otherwise returns -1.
syntax: arry.IndexOf(item , indexStart)
indexStart can be omitted.
What does
.include()
do?
Looks for item starting from indexStart, returns true if found.
syntax: arr.include(item, indexStart)
indexStart can be omitted.
What does
find()
do?
Looks for an object with a specific condition, if found it returns true and the item is returned. If item is not found, ‘undefined’ is returned.
syntax: arr.find(function(item, index, array){});
example: users.find(item => item.id == 1);
What
does findIndex()/findLastIndex()
do?
Similar to find(), it finds the first of an element, but returns the index, rather than the item itself.
syntax: arr.findIndex(item, index, array)
What does filter(func) do?
Similar to find(), but instead of the first, and 1 item it encounters, it returns an array of all matching elements.
syntax: arr.filter(function(item,index,array){});