L1- Array Methods Flashcards
What does forEach return?
Undefined
Does .filter mutate?
no
Does .filter use a callback method?
yes. It examinds the return from each iteration for truthiness.
Does .map use a callback method?
Yes, whatever is return is push to a new array.
Can the array returned from .map be shorter than the caller?
no, it will be the same length even if elements have to be undefined.
EVEN IF the callback function changes the length of the original. But then it still creates empty elements and counts toward the length
Return true if at least 1 array element returns true from a callback function
SIMPLE LANGUAGE:
Do some of the array element follow this pattern?
myArray.some( )
What’s the difference between .some() and .includes()?
.includes() is just a simple comparison of the argument and array values.
.some() lets you use a callback function to test whatever logic you want.
What is the difference between each of these (what do they do and return?) **WHat do they return if they don't find the thing they're looking for? Array.prototype.includes() Array.prototype.some( ) Array.prototype.every() Array.prototype.filter() **(no elements return truthy value) Array.prototype.find() ** Array.prototype.indexOf() ** Array.prototype.findIndex() **
.includes() - returns boolean based on whether the argument is within the array
.some( ) - iterates over each element and returns boolean if at least 1 iteration returns true based on a callback function
.every() - iterates over each element and returns boolean if all iterations return true based on a callback function
.filter() - iterates over each element and returns an array based on whether the elements evaluate to true based on a callback function
**empty array
.find() - returns the value of the first element that returns true from a callback function
**Undefined
.indexOf() returns the first index of an element that matches the argument
**-1
Array.prototype.findIndex() returns the index of the first element that returns true from a callback function
**-1
What does
Array.prototype.some()
do?
and/or return?
“Are there some values in the array for which the given callback returns a truthy value?
tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.
// Arrow function some((element) => { /* ... */ } ) some((element, index) => { /* ... */ } ) some((element, index, array) => { /* ... */ } )
// Callback function
some(callbackFn)
some(callbackFn, thisArg)
// Inline callback function some(function(element) { /* ... */ }) some(function(element, index) { /* ... */ }) some(function(element, index, array){ /* ... */ }) some(function(element, index, array) { /* ... */ }, thisArg)
What does
Array.prototype.every()
do?
and/or return?
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
// Arrow function every((element) => { /* ... */ } ) every((element, index) => { /* ... */ } ) every((element, index, array) => { /* ... */ } )
// Callback function
every(callbackFn)
every(callbackFn, thisArg)
// Inline callback function every(function(element) { /* ... */ }) every(function(element, index) { /* ... */ }) every(function(element, index, array){ /* ... */ }) every(function(element, index, array) { /* ... */ }, thisArg)
Return true if all array elements returns true from a callback function
SIMPLE LANGUAGE:
Does every array element follow this pattern?
myArray.every()
Returns the first element that returns true from a callback function.
SIMPLE LANGUAGE:
What’s the first element in the array that follows this pattern?
myArray.find( callback fuction)
What does
Array.prototype.find()
do? and/or return?
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
// Arrow function find((element) => { /* ... */ } ) find((element, index) => { /* ... */ } ) find((element, index, array) => { /* ... */ } )
// Callback function
find(callbackFn)
find(callbackFn, thisArg)
// Inline callback function find(function(element) { /* ... */ }) find(function(element, index) { /* ... */ }) find(function(element, index, array){ /* ... */ }) find(function(element, index, array) { /* ... */ }, thisArg)
Returns the index of the first element in the array that satisfies the provided testing/callback function. Otherwise, it returns -1, indicating that no element passed the test.
Array.prototype.findIndex()
What does:
Array.prototype.findIndex()
do and/or return?
Returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
Reverse an array’s elements in place (mutating).
It also returns the reversed array
Array.prototype.reverse( )
What does
Array.prototype.reverse()
do?
and/or return?
reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
Mutates
Returns the reversed array
reverse()