Answers for "reduce js usage"

90

javascript reduce

var array = [36, 25, 6, 15];

array.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue;
}, 0); // 36 + 25 + 6 + 15 = 82
Posted by: Guest on May-21-2020
8

reduce javascript

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;
Posted by: Guest on March-03-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language