Search code examples
javascriptarraysreduce

How to sum specific element in reduce object?


Lets say I have this array of objects:

const arrayOfObjects = [
  { task: "work", time: 1 }, 
  { task: "travel", time: 4 }, 
  { task: "work", time: 5 }, 
  { task: "eat", time: 3 }, 
  { task: "eat", time: 1 }, 
  { task: "eat", time: 5 }
];

and I want to return a single objects that returns each key as the task and each value as the sum of all values of the key. for example, the produced object of the array above should be:

sumOfObejcts = {
  work: 6,
  travel: 4,
  eat: 9
}

How can I do it properly with reduce function? I don't know how to sum all the items of specific key, this is what I made after few tries from examples:

    const sumOfObejcts = arrayOfObjects.reduce((acc, items) => {
      let { task, time } = items;
      return { ...acc, [task]: [...(acc[task] || []), time] };
    }, {});

and the output I get is:

{
  work: [1, 5],
  travel: [4],
  eat: [3, 1, 5]
}

So, I just want to return the sum of occurrence of the value instead.


Solution

  • Your solution is very close; except that you're creating array of values on each iteration, instead of summing the current time values.

    Here I've changed the value you assign to the key by using a ternary statement. This checks to see if the task exists in the accumulator object; if the task already exists, that means there is already a sum for this task and thus we just need to add on the current time to the existing sum. Otherwise, if the accumulator object doesn't have the task, the value will be primed using the current task's time.

    const sumOfObjects = arrayOfObjects
      .reduce((acc, item) =>
        ({ ...acc, [item.task]: (
           acc[item.task] // does the task exist in the accumulator object?
           ? acc[item.task] + item.time // if so, set a value equal to the current task's time plus the existing value
           : item.time // otherwise, prime the task's value to the current time
          ) })
      , {});