cachaito
2/18/2016 - 9:21 AM

Reduce explanation

var items = [{price: 10}, {price: 120}, {price: 1000}];

// our reducer function
var reducer = function add(sumSoFar, nextPrice) { 
  return sumSoFar + nextPrice.price;
};

// do the job
var total = items.reduce(reducer, 0);

console.log(total); // 1130

var total = items.reduce(reducer, -20);

console.log(total); // 1110
const arr = [108, 609, 232, 787];
const initialInfo = {totalScore: 0, itemCount: 0};

// Funkcja reducera nie musi posiadać 'accumulator' z jednym argumentem:
function addScores({totalScore, itemCount}, score) {
    return {
      totalScore: totalScore + score,
      itemCount: itemCount + 1,
    };
}

const result = arr.reduce(addScores, initialInfo);