Map and Reduce Methods
Exploring the map and reduce methods of the array.
In this lesson, we’ll look at two unique array methods, map
and reduce
, that can make arrays even more convenient.
Introduction
The map
and reduce
methods are used to get a modified version of the array or a reduced value using callback functions. Let’s look at the two methods individually.
map
method
The map
method applies a function to each element of the array and creates a new array of the returned values. Now, we can look at its uses.
Syntax of map
method
The syntax of the map
method looks like this:
arr.map(<function>);
The map
method above will take, as an argument, a function invoked for each element in the array. The function that will be passed is given arguments by the map
method in the following order.
function callbackfn(value: any, index: number, array: any[])
Again, for each element the callbackfn
will be passed with the value of the element as the first argument, followed by the index of the element as the ...