Describing the reduce
Operation
- The
reduce
operation is used for testing if every element of an array satisfies a condition -
It accepts a callback function with the following arguments:
accumulator
: Accumulated value previously returnedcurrentValue:
Current element processed in the arrayindex
: Optional index ofcurrentValue
array
: Optional array that is calledthisArg
: Optional value to use asthis
Defining an Array
let fruits = ["Apple", "Banana"];
console.log(fruits.length);
// 2
Using the reduce
Operation
// Regular function
fruits.reduce(function(accum, item) {
return accum + item;
})
// "AppleBanana"
// Arrow function
fruits.reduce((accum, item) => accum + item);
// "AppleBanana"
Previous