Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

javascript - 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.

question from:https://stackoverflow.com/questions/65872353/how-to-sum-specific-element-in-reduce-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
      ) })
  , {});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...