JS Array Methods Flashcards

Practice remembering what specific array methods can do, and what situation it may be ideal to use over another.

1
Q

What does
~~~
pop()
~~~
do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does
push()
do?

A

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’, …);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does
~~~
shift()
~~~
do?

A

Removes and returns the first element of an array. All subsequent elements shift down one place.

no arguements needed. Alters original array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does
~~~
unshift()
~~~
do?

A

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, …);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does
~~~
.slice()
~~~
do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does
.splice()
do?

A

swiss army method - it can insert, remove, and replace elements.

syntax: arr.splice(starting index, deleteCount, newInsertedElement(s),…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does
.slice()
do?

A

Returns a new array, copying elements from startIndex, and endIndex(not including end) arguements.

syntax: arr.slice(startIndex, endIndex)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does
.concat()
do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In order to use .concat() on an array-like Object, how can we have said object treated as an array by .concat()?

A

If the object has the property of :
\[Symbol.isConcatSpreadable]: true
it’s elements will be added with the behavior of normal arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is
.forEach()?

A

.forEach() method is used to iterate/run a function for every element of the array

syntax: arr.ForEach(function(element, index, array){}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does
.indexOf()
do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does
.include()
do?

A

Looks for item starting from indexStart, returns true if found.
syntax: arr.include(item, indexStart)
indexStart can be omitted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does
find()
do?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What
does findIndex()/findLastIndex()
do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does filter(func) do?

A

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){});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does .map(func) do?

A

It calls the functon for each element of the array, and returns the array of results.

syntax: arr.map(function(item, index, array){});

17
Q

what does
.sort(func)
do?

A

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.

18
Q

What does .reverse() do?

A

reverse the order of elements in an array.

no parameters used.

19
Q

What does str.split(delim) do?

A

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.

20
Q

What does join() do?

A

creates a string using elements joined by provided parameter.
ex: arr.join(;) results in “item1;item2;item3”

21
Q

What do
reduce()/reduceRight()
do?

A

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.

22
Q

How can we determine if an item/obj is an array?

A

using .isArray will return true or false.

23
Q

How can we think of
.some(fn) / .every(fn)
as?

A

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.

24
Q

What is arr.fill?

A

method used for filling the array with repeated values from indexStart to indexEnd
syntax: arr.fill(value, start, end)

25
Q

what’s the difference between .find() and filter()?

A

.find()’s return value returns undefined if no match.