Array Callback Methods are methods that are built specifically to works with arrays. These methods use the arrow function syntax.
Here are some of these methods:
Colt Steele
Array Callback Methods are methods that are built specifically to works with arrays. These methods use the arrow function syntax.
Here are some of these methods:
forEach() expects a function as an argument, then runs that function, for each element in an array. The forEach() method does not run if the array is empty. Example:
*note: forEach() is not used that much anymore, now we use for Of.
map() creates a new array with the results of running a "callback function" on every element in the original array. map() does not execute the function for empty elements. map() does not change the original array.
So map(), takes the original array, changes it in a "callback" function, and returns the results as a new array. Example:
So map() is generally used when you want to change every element of an array, in some fashion. Map creates a new array when run but will not run on an empty array.
filter(): method creates a new array filled with elements that pass a test provided by a function. This will test every element in the array and return either true or false. If the element returns true, it is added to the new array.
lets create an array to work with:
If we create a filter method to return the numbers in the array that are odd, it could look like this:
and if we print out odds, it would return:
*note that this does not change the original array. nums is still equal to [9, 8, 7, 6, 5, 4, 3, 2, 1]. Instead, filter creates a new array, odds that holds the returns from the callback function [9, 7, 5, 3, 1].
some(): Always returns true or false. This method checks if any array element passes a test (provided as a callback function). The some() method executes the callback function once for each array element, returns true (and stops) if the function returns true for any one of the array elements. The some() method returns false if the function returns false for all of the array elements. The some() method does not execute the function for empty array elements. The some() method does not change the original array.
Example: const exams = [80, 82, 92, 78, 70, 90, 89, 81, 77];
if we run
but if we run
every(): Always returns true or false. This method executes a function (provided as a callback function) for each array element, and returns true if the function returns true for all elements. The every() method returns false if the function returns false for one element. The every() method does not execute the function for empty elements. The every() method does not change the original array
Example: const exams = [80, 82, 92, 78, 70, 90, 89, 81, 77];
if we run
but if we run
==========================================================
Another example:
Say we wanted to pass several arrays into a function for processing. Let's use three "number" arrays and test to see if all of the numbers in each array are even. A Callback Function would be perfect for this. Remember, a Callback Function is a function that takes a function as an argument.
Lets set up our basic function:
Let's set up our arrays for testing:
In the Callback Function, we will use every() to test if EVERY element in the array, is an even number:
|
So we have an array and we want to check if all the elements are even numbers [1,4,6,8]
We want to pass this array into our allEvens() function like so: allEvens([1,4,6,8]); and the array is passed into the function variable, arr
The callback function then runs the every() method on arr (array passed in) and because of the nature of the every() method will return true or false based on the test set on the next line.
The every() method then test's every item in the array (arr) and returns true or false depending whether or not num % 2 === 0, which is then returned to the Callback Function which in turn returns the results (true or false) to the initial function called, allEvens().
So for our three arrays, this would be the returned results:
*note: we could also use an arrow function to simplify our code like so:
|
reduce(): method executes a reducer function for array element and returns a single value, the function's accumulated result. Essentially, this method reduces an array down to a single value. The reduce() method does not execute the function for empty array elements. The reduce() method does not change the original array.
The basic syntax is:
This works extremely well if want to add an array full of numbers. For example:
This returns 35 because 3 + 5 = 8 + 7 = 15 + 9 = 24 + 11 = 35, thus reducing the array down to a single element which is the sum of all of the elements.
*note that this does not change the original array.
You can also shorten and simplify the code with an Implicit Return and an Arrow Function:
nums.reduce((total, currentValue) => total + currentValue); returns the same result 35.
You are not limited to addition, you could perform any mathematical function:
You could aslo add the optional initialValue paramater to alter the reduce, like so:
You are also not limited to mathematical functions. You could do anything that would return a single value such as find the lowest, or highest number.
This will obviously return 3. It runs through the Callback Function starting with 3 and compares it to the next number in the list until it reduces the entire array down to a single return, in this case, the lowest number.
find(): method returns the value of the first element that passes a test. The find() method executes a function for each array element. The find() method returns undefined if no elements are found. The find() method does not execute the function for empty elements. The find() method does not change the original array.