Lodash教程

Lodash reduce method

语法:

_.reduce(collection, [iteratee=_.identity], [accumulator])
Reduces 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. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection).

参数:

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

输出:

(*) − Returns the accumulated value.

例子:

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.

命令:

\>node tester.js

输出:

10
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4