reduce/reduceRight

使用 reduce 和 reduceRight 方法可以汇总数组元素的值,具体用法如下:

reduce

function appendCurrent (previousValue, currentValue) {
    return previousValue + "::" + currentValue;
}
var elements = ["abc", "def", 123, 456];
var result = elements.reduce(appendCurrent);
document.write(result); //abc::def::123::456

reduceRight

function appendCurrent (previousValue, currentValue) {
    return previousValue + "::" + currentValue;
}
var elements = ["abc", "def", 123, 456];
var result = elements.reduceRight(appendCurrent);
document.write(result); //456::123::def::abc

 

posted @ 2019-03-12 13:45  空瓶子装满了  阅读(236)  评论(0编辑  收藏  举报