The map() method is used for creating a new array from an array and applying a function to each one of the elements of an array.
In the callback generally, the array element is required. Some action is performed to the value and the new value is returned.
Let us understand it with the help of an example:
A filter method is used to take each element in an array and it applies a conditional statement against it. If the conditional statement returns true, then the elements get pushed to the output array. If the conditional statement returns false, then the elements don’t get pushed to the output array.
The syntax of map and filter is very similar, except the callback function should return true to keep the element or false otherwise.
A reduce method is generally used to reduce an array of values just down to one value. For getting the output value it runs a reducer function on each element of an array.
The callback argument is a function that will be called once for every item in an array. The function generally takes four arguments but often only the first two are used.
- accumulator: It is used to return the value of the previous iteration.
- currentValue: It is generally used for the current item in an array
- index: It is used for the index of the current item
- array: The original array on which reduce was called.