_.reduce(数组或对象迭代累加后的结果)
语法:
_.reduce(collection, [iteratee=_.identity], [accumulator])源代码链接:source
npm包链接:npm package
描述:
压缩collection(集合)为一个值,通过iteratee(迭代函数)遍历collection(集合)中的每个元素,每次返回的值会作为下一次迭代使用(注:作为iteratee(迭代函数)的第一个参数使用)。 如果没有提供accumulator,则collection(集合)中的第一个元素作为初始值。(愚人码头注:accumulator参数在第一次迭代的时候作为iteratee(迭代函数)第一个参数使用。) iteratee 调用4个参数:(accumulator, value, index|key, collection).
lodash 中有许多方法是防止作为其他方法的迭代函数(注:即不能作为iteratee参数传递给其他方法),例如:_.reduce,_.reduceRight, 和_.transform。
受保护的方法有(注:即这些方法不能使用_.reduce,_.reduceRight, 和_.transform作为 iteratee 迭代函数参数):
assign,defaults,defaultsDeep,includes,merge,orderBy, 和sortBy。
开始版本:0.1.0
参数:
collection (Array|Object): 用来迭代的集合。[iteratee=_.identity] (Function): 每次迭代调用的函数。[accumulator] (*): 初始值。
返回值:
(*): 返回累加后的值。
例子:
_.reduce([1, 2], function(sum, n) {
return sum + n;
}, 0);
// => 3
_.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
return result;
}, {});
// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)Last updated
Was this helpful?