Search

5/31/2014

Array.prototype.reduce() - JavaScript | MDN

Array.prototype.reduce() - JavaScript | MDN

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value.
var total = [0, 1, 2, 3].reduce(function(a, b) {
    return a + b;
});
// total == 6
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
    return a.concat(b);
});
// flattened is [0, 1, 2, 3, 4, 5]

沒有留言: