Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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;
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #reduce #javascript
ADD COMMENT
Topic
Name
5+1 =