001 Array Callback Methods
001 Array Callback Methods
CALLBACK
METHODS
ARRAY
CALLBACK METHODS
Arrays come with many
built-in methods that
accept callback functions
Now that we've covered
callbacks, we can learn
these methods!
SUPER USEFUL!
GOALS
Use the new arrow function syntax
Understand and use these methods:
forEach
map
filter
find
reduce
some
every
FOREACH
Accepts a callback
function.
Calls the function
once per element
in the array.
MAP
Creates a new array with
the results of calling a
callback on every element
in the array
MAP
ARROW
FUNCTIONS!
ARROW
FUNCTIONS
"syntactically compact alternative"
to a regular function expression
ARROW
FUNCTIONS
IMPLICIT RETURN
All these functions do the same thing:
FIND
returns the value of the first element in the array
that satisfies the provided testing function.
FILTER
Creates a new array with all elements that pass the
test implemented by the provided function.
EVERY
tests whether all elements in the array pass the
provided function. It returns a Boolean value.
SOME
Similar to every, but returns true if ANY of
the array elements pass the test function
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
REDUCE
Executes a reducer function
on each element of the array,
resulting in a single value.
SUMMING AN ARRAY
FINDING MAX VAL
INITIAL VALUE
TALLYING
SORT PT. 2
arr.sort(compareFunc(a,b))
If compareFunc(a,b) returns less than 0
Sort a before b
If compareFunc(a,b) returns 0
Leave a and b unchanged with respect to
each other
If compareFunc(a,b) returns greater than 0
Sort b before a
SORT