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){});
What does .map(func) do?
It calls the functon for each element of the array, and returns the array of results.
syntax: arr.map(function(item, index, array){});
what does
.sort(func)
do?
sorts the elements in place, changing its element order. The original array is modified.
parameter/method of sorting can be your own function to establish rules of sorting order.
by default, items are sorted as strings.
note: using .localeCompare method to correctly sort letters.
What does .reverse() do?
reverse the order of elements in an array.
no parameters used.
What does str.split(delim) do?
Used on a string variable, and used delimiting character to split the string into an array.
ex: str.split(‘, ‘) will split words seperated by a comma into elements in an array.
What does join() do?
creates a string using elements joined by provided parameter.
ex: arr.join(;) results in “item1;item2;item3”
What do
reduce()/reduceRight()
do?
Its a interesting tool used to calculate a single value based on an array.
syntax: arr.reduce(function(accumulator, item, index, array){},[initial]);
ex:
arr.reduce((sum, current) => sum + current, 0);
can be used to add up the values of an array, using 0 as the initial starting value.
How can we determine if an item/obj is an array?
using .isArray will return true or false.
How can we think of
.some(fn) / .every(fn)
as?
Similar to || and && operators.
.some will return true of any case is true, while .every will only return true if every item case is true.
What is arr.fill?
method used for filling the array with repeated values from indexStart to indexEnd
syntax: arr.fill(value, start, end)