Array Methods Flashcards
Check whether a thing is an array
SIMPLE LANGUAGE:
Is the argument an Array?
Array.isArray(input)
Iterate over each array element. The return from a callback function is passed to the next iteration.
SIMPLE LANGUAGE:
Analyze all array elements, allow a variable to pass from one iteration to the next
myArray.reduce(callback function(acc, arg) => {
acc += arg}, accStart
)
ex.
> let arr = [2, 3, 5, 7]
> arr.reduce((accumulator, element) => accumulator + element, 0)
let apple = [1, 2, 3, 4, 5, 6]
console.log(apple.reduce((acc, value) =>
[(acc[0] + value), (acc[1] - value)], [0,0] ));
logs: [ 21, -21 ]
Analyze all of an array’s elements, returns the elements in a new array based on whether a function for each element is true.
SIMPLE LANGUAGE:
Create a new array from old array filtered based on some condition.
myArray.filter(element => if true return element as part of new array)
Return boolean if a string contains a specified substring
or
if an array element matches the entirety of the argument
SIMPLE LANGUAGE:
Does the array contain this entry?
Does the string contain this substring?
myStringOrArray.includes(‘value’)
SIMPLE LANGUAGE:
Cut (mutate) an array and return the cut section
myArray.splice(start index, number of indexes)
SIMPLE LANGUAGE:
Add argument to the end of an array and return the new array length
myArray.push(new element);
SIMPLE LANGUAGE:
Add argument to the beginning of an array and return the new array length
myArray.unshift(new element);
Return a combined array or string from the argument.
Returns string if method is used on a string.
Returns array if method is used on an array.
SIMPLE LANGUAGE:
Add string arguments to the end of a string
Add arrays, or other arguments to the end of an array.
myStringorArray.concat( , )
does not mutate.
Remove the last element from an array and return the element
myArray.pop();
Remove the first element from an array and return the element
myArray.shift();
Return a copied section from an array (or string)
What happens if the first argument equals the second?
What happens if there is no second argument?
myArray.slice(first index, last index exclusive)
If first index = last index, an empty array is returned
It returns an array/string from that index to the last
Reorganize an array’s elements in place (mutating). It also returns the organized array
No arg defaults to string comparison
myArray.sort( )
Reverse an array’s elements in place (mutating).
It also returns the reversed array
Array.prototype.reverse( )
Return the Array with nested arrays unnested.
can choose level of unesting
myArray.flat( number of levels to unnest)
Return combined array elements into a string with argument in between
myArray.join(‘ ‘)
What’s the difference between Array.from and .map() ?
Array.from() works with array-like objects like strings
Array.from() is meant to create arrays from array like objects and can take a callback function
.map() is for arrays
.map() is meant to map a new array off an existing one using a callback function. It has more functionality
.map() is 15 times faster
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( )
Return true if all array elements returns true from a callback function
SIMPLE LANGUAGE:
Does every array element follow this pattern?
myArray.every()
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.
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)
Replace an array’s elements between certain indexes with a SINGLE entry
Returns the changed array
SIMPLE LANGUAGE
Replace each consecutive elements with a 1 argument.
myArray.fill(replacement, start ind, end exclusive index)
mutates
[1, 2, 3, 4, 5, 6, 7, 8].fill(“apple”, 4, 6)
Expected output:
[ 1, 2, 3, 4, ‘apple’, ‘apple’, 7, 8 ]
Return the Array elements based on a callback function and with first-level nested arrays unnested.
Does it map first or flat first?
SIMPLE LANGUAGE
Map an array based on callback and flat the result
myArray.flatMap( callback function)
It maps then flats.
What is returned from:
[1,2].concat([3,4]);
[1,2].concat([[3,4]]);
‘string’.concat(342432)
output=> [1,2,3,4]
output=> [1,2,[3,4]]
output=> “string342432”
What is returned from:
[1, 2, 3].pop().shift()
an error
Because it is evaluated from left to right
[1, 2, 3].pop() evaluates to 3
and 3.shift() give an error
What are the similarities and difference between .substring() and .slice()?
What happens when only 1 argument?
If start index > end index, what happens?
What do indexes start and finish?
what do they work on?
What happens when second argument larger than length?
how do they repond to negative numbers or NaN?
Similarities
- Both go from start index (inc) to end index (exc)
- If end index is excluded they go to the end
- If end index > length, end index = length;
Differences
- slice works with strings and arrays, substring only works on strings.
- If start index > end index, .slice() will return empty string/ array. .substring() will swap the indexes.
- both act different with negative arguments and NaN
https: //stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring
What is logged to console from this:
let apple = [1, 2, 3, 4, 5, 6]
console.log(apple.reduce((acc, value) =>
[(acc[0] + value), (acc[1] - value)], [0,0] ));
Why?
logs: [ 21, -21 ]
The accumulator is set to: [0,0]
The callback function returns 2 elements in an array
(acc[0] + value) and (acc[1] - value)
The first accumulator element plus the value
The second accumulator element minus the value