JS array methods Flashcards

1
Q

What are the three ways we can iterate over an array?

A

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
}  ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do push() and unshift() do?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do push() and unshift() methods return? and does it modify an existing array

A

Both also return, specifically, the new length of the array.both push() and unshift() modify existing arrays.

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

What are pop() and splice() used for?

A

both can be used methods to remove elements from an array.

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

What does pop() do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does shift() do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Break down the slice() method

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Break down the splice() method.

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Break down the slice() method.

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does concat() method do?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Breakdown forEach()

A

forEach() allows a function to run for every element of an array
syntax: forEach(function (item, index, array) ){ //..function to run on elements}

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

Explain indexOf() and lastIndexOf()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does include() do?

A

Checks to see if an item exists in an array. returns true / false.
syntax: include(item, from) //starts searching at -from- index

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

explain find() method

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to determine use cases for findIndex(), indexOf(), include(), some(), etc

A

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.

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

Breakdown findIndex() / findLastIndex()

A

similar to find() however instead of returning truthy value, it returns the index of found items (or -1 if not found).

						syntax: same sytax as find()
								findIndex / findLastIndex( (element)=>{})    //current element being processed in the array
								findIndex / findLastIndex( (element, index, array)=>{}) //the array find() was called upon
								findIndex / findLastIndex(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
								findIndex / findLastIndex(callbackFn, thisArg)   //a value to use -this- when executing callbackFn (see iterative methods)
								findIndex / findLastIndex( (element, index)=>{}) //index of the current element being processed in the array
17
Q

what does filter() do?

A

similar to find, but filter() returns a shallow copy array of all matching elements. (if no element matches, empty array is returned.)

						syntax: filter( (element)=>{})    //current element being processed in the                         array
										filter( (element, index)=>{}) //index of the current element being processed in the array
										filter( (element, index, array)=>{}) //the array find() was called upon
										filter(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
										filter(callbackFn, thisArg)   //a value to use -this- when executing                             callbackFn (see iterative methods)
18
Q

what does map() do?

A

map() calls a function for each element of the array, and returns the array of results.
note: map() is a copy method, only mutates original array if it’s an assignment to it.

						syntax: map( (element)=>{})    //current element being processed in the                 array
										map( (element, index)=>{}) //index of the current element being                             processed in the array
										map( (element, index, array)=>{}) //the array find() was called upon
										map(callbackFn)    //a function to execute for each element in the                           array. Its return value is added as a single element in the new array
										map(callbackFn, thisArg)   //a value to use -this- when executing                             callbackFn (see iterative methods)
19
Q

what does sort() do?

A

sort() sorts the elements in place, and returns the reference to the same array.
by default, sort order is ascending string values. Almost always want a function to provide.
note: orginal array is sorted in place, no copy is made.

						syntax: sort() //functionless - string sorting order.
										sort( (a, b) =>{})  //arrow function - a is first element for comparison,                       b is second element to compare.
										sort(compareFn)     //compare function - specifies a function that defines the sort order.
										sort(function compareFn( a, b ) {}) //inline compare function
20
Q

what is the function to provide to sort() in order to numerically sort?

A

sort( (a, b) => a - b);

21
Q

What does reverse() do?

A

reverse the order of elements in array. (no parameters.)

22
Q

Breakdown split() method.

A

Split takes a string and uses delimiter character to seperate into substrings
substrings are put into an array, which is returned.

syntax: split() //if arguements are ommitted, string is returned wrapped in an array.
                        split(seperator) //seperate is a character used to know how to split string
                        split(seperator, limit)//none-negative int specifying limit on number of substrings to be in array. leftovers not included.
23
Q

Breakdown join() method.

A

join() creates and returns a new string by concatenating all elements in an array
seperated by commas, or a specified glue string. If only 1 item, item will be returned without glue string.

						syntax: join()//all elements joined without any characters inbetween                       them.
										join(glue)//glue is a provided string/character that the elements will                       be joined inbetween.
24
Q

When to use forEach(), for, for..of VS map() VS reduce()

A
  forEach(), for, for..of isi used when we need to iterate over an array.
           map() is used when we need to iterate and return the data for each                       element.
           reduce() is used when we need to calculate a single value based on an                 array.
25
Q

Breakdown reduce() /reduceRight()

A

easiest example to grasp is using reduce’s accumulator to accumulator the total value of all values in a given array.

				syntax: reduce ( (  accumulator, currentValue*, currentIndex*, array*){},                       initialValue)
						/ /accumulator: result of previous function call, equals initialValue if                           provided.
						/ /item: current array item
						/ /index: position of item
						/ /array: is the array.
26
Q

what is “thisArg” parameter found in most methods?

A

thisArg becomes ‘this’ for functions.

27
Q

what does some() do?

A

some() tests whether atleast one element in array passes the provided function test. Returns true/false.

						 syntax: some( (element)=>{})                    //current element
										some( (element, index) =>{})            //index of current element
										some( (element, index, array)=>{})      //the array some() was called                         upon
										some(callbackFN, thisArg*) //function to exe for each element,                               should return true/false
28
Q

what does every() do?

A

every() tests whether ALL elements in the array pass test function. Returns true/false.

						syntax: every( (element)=>{})                    //current element
										every( (element, index) =>{})            //index of current element
										every( (element, index, array)=>{})      //the array some() was called                         upon
										every(callbackFN, thisArg*) //function to exe for each element,                               should return true/false