Node.js lodash reduce() sum array

Description

Node.js lodash reduce() sum array


var _ = require('lodash');
var numbers = [1,2,3,4,5,6,7,8,9,10];

var result = _.reduce(numbers, function(sum, num) {
  return sum += num;
});//  w  w  w .  j a  va  2 s  . c  o  m

console.log(result);

More example


//returns accumulated value

var lodash = require("lodash");
lodash.reduce([1,2], function(total, n) {
  return total+n;
});//from   w  w  w .  j a v a2  s  . c o m
// gives 3

lodash.reduce({'a': 1, 'b': 2}, function(result, n, key) {
  result[key] = n * 3;
  return result;
}, {});
// gives {'a': 3, 'b':6}



PreviousNext

Related