Lodash - reduceRight 方法



語法

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

此方法類似於 _.reduce,不同之處在於它從右到左迭代集合的元素。

引數

  • collection (陣列 | 物件) − 要迭代的集合。

  • [iteratee=_.identity] (函式) − 每次迭代時呼叫的函式。

  • [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 ]
lodash_collection.htm
廣告
© . All rights reserved.