Lodash - reduceRight 方法
语法
_.reduceRight(collection, [iteratee=_.identity], [accumulator])
此方法与 _.reduce 类似,不同之处在于它从右到左迭代集合中的元素。
参数
collection (Array|Object) − 要迭代的集合。
[iteratee=_.identity] (Function) − 每次迭代调用的函数。
[accumulator] (*) −初始值。
输出
(*) − 返回累积值。
示例
var _ = require('lodash'); var list = [[0, 1], [2, 3], [4, 5]]; var result = _.reduceRight(list, function(flattened, other) { return flattened.concat(other); }, []); console.log(result);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
[ 4, 5, 2, 3, 0, 1 ]