Lodash – reduce method

  • Post author:
  • Post category:Lodash
  • Post comments:1 Comment
Lodash - reduce method

Syntax Of Lodash reduce method

_.reduce(collection, [iteratee=_.identity], [accumulator])

Lodash reduce method of collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous.

Arguments

  • collection (Array|Object) โˆ’ The collection to iterate over.
  • [iteratee=_.identity] (Function) โˆ’ The function invoked per iteration.
  • [accumulator] (*) โˆ’ The initial value.

Output

  • (*) โˆ’ Returns the accumulated value.

Example

var _ = require('lodash');
var list = [1, 2, 3, 4];
var result = _.reduce(list, function (sum, n) {
   return sum + n;
}, 0);
console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

10

Next Topic – Click Here

This Post Has One Comment

Leave a Reply