mic*_*lle 1 javascript underscore.js
我是Underscore.js的新手.
最近我阅读了有关reduce和reduceRight 的文档,但我无法理解这两者之间的区别.
我将不胜感激任何帮助和榜样.
好吧,_.reduce从第一个索引开始遍历集合并在最后一个索引上完成,同时_.reduceRight执行相同的操作但从最后一个索引开始并在第一个索引上完成.
var list = ['a', 'b', 'c'];
_.reduce(list, function(memo, item) { memo.push(item); return memo; }, []);
=> ['a', 'b', 'c']
_.reduceRight(list, function(memo, item) { memo.push(item); return memo; }, []);
=> ['c', 'b', 'a']
Run Code Online (Sandbox Code Playgroud)