xxxxxxxxxx
let arr = [1, 2, 3, 4, 5];
let newArr = arr.reduce((item1,item2) => {
return item1 + item2;
})
console.log(newArr)
// output 15
// 1+2+3+4+5
xxxxxxxxxx
var array = [36, 25, 6, 15];
array.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0); // 36 + 25 + 6 + 15 = 82
xxxxxxxxxx
const arr = [1, 2, 3]
const result = arr.reduce((acc, curr) => acc + curr, 0)
// 1 + 2 + 3 = 6
console.log(result)
xxxxxxxxxx
let array = [36, 25, 6, 15];
array.reduce((acc, curr) => acc + curr, 0)
// 36 + 25 + 6 + 15 = 82
xxxxxxxxxx
var depthArray = [1, 2, [3, 4], 5, 6, [7, 8, 9]];
depthArray.reduce(function(flatOutput, depthItem) {
return flatOutput.concat(depthItem);
}, []);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9];
// --------------
const prices = [100, 200, 300, 400, 500];
const total = prices.reduce((total, cur) => {return total + cur} )
// init value = 0 => total = 1500;
// --------------
const prices = [100, 200, 300, 400, 500];
const total = prices.reduce(function (total, cur) => {
return total + cur
}, 1 )
// init value = 1 => so when plus all numnber will be result => 1501;
// --------------
const prices = [100, 200, 300, 400, 500];
const total = prices.reduce((total, cur) => total + cur )
// 1500;
Reduce in js
xxxxxxxxxx
let arr=[5,6,2];
let sum=arr.reduce((accumulator,current,index,arr)=>{
return accumulator+=current;
});
console.log(sum);//5+6+13
accumulator acumulate(hold) the sum output
reduce methood creat a new array and does not change actual array
xxxxxxxxxx
const sum = array.reduce((accumulator, element) => {
return accumulator + element;
}, 0);
xxxxxxxxxx
[3, 2.1, 5, 8].reduce((total, number) => total + number, 0)
// loop 1: 0 + 3
// loop 2: 3 + 2.1
// loop 3: 5.1 + 5
// loop 4: 10.1 + 8
// returns 18.1
xxxxxxxxxx
n JavaScript, the reduce() method is used to apply a function to each element of an array and reduce it to a single value. The reduce() method takes two parameters: a callback function and an optional initial value.
Here is the basic syntax of the reduce() method:
'array.reduce(callback, initialValue)'
The callback function takes four parameters: accumulator, currentValue, currentIndex, and array. The accumulator is the value returned from the previous invocation of the callback, or the initialValue if provided. The currentValue is the current element being processed, currentIndex is the index of the current element, and array is the array on which reduce() was called.
The reduce() method executes the callback function once for each element in the array (excluding the initial value if provided), and the return value of the callback becomes the new value of the accumulator.
Here is an example that demonstrates the usage of reduce() to calculate the sum of an array of numbers:
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(sum); // Output: 15
In the example above, the reduce() method is called on the numbers array. The callback function takes two parameters, accumulator and currentValue, and simply adds them together. The initial value of the accumulator is set to 0 using the second argument of reduce(). The final result, 15, is the sum of all the numbers in the array.