Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
How to reduce objects in an array based off an existing key value

So I have an existing array with three objects:

              [ { date: 3/12/2022, oService: 10}
                { date: 3/13/2022, oService: 12, aService: 1}
                { date: 3/13/2022, oService: 1 }]

Based on the date, I would like to get something that looks like the following:

                [ {date: 3/12/2022, oService: 10}
                  {date: 3/13/2022, oService: 13, aService: 1}]

additionally, I could have up to 10 services, so I cant reduce by prev.oService. I'll have an array of services that looks something like:

const Services = [aService, bService, cService, oService, yService]

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think it's best to think of the problem as 2 different steps:

  • First, get all things that belong to the same date together. This is a groupBy that you should be able to look up on StackOverflow or borrow from an existing library.
  • Then, you merge each of the date groups in to a single object.

The merge operation is a bit more custom than the groupBy operation. You can implement it in many ways. I would propose to:

  • Implement merge to deal with just 2 objects
  • Inside merge, loop over all unique keys in those objects and sum them (excluding the date key)
  • To apply merge to a date group, use reduce. Note that it's safe to not provide a seed argument here, because you are guaranteed to not have empty groups.

// Merge two date-service entries in to one
const merge = (a, b) => {
  const merged = Object.assign({}, a);
  
  Object.keys(b).forEach(k => {
    if (k !== "date")
      merged[k] = (merged[k] || 0) + b[k];
  });
  
  return merged;
};

const input = [ { date: "3/12/2022", oService: 10},
                { date: "3/13/2022", oService: 12, aService: 1},
                { date: "3/13/2022", oService: 1 }];
          
// Create groups that have a matching date
const byDate = groupByProp("date", input);

// Reduce each group to a single item by merging
const all = Object.values(byDate).map(xs => xs.reduce(merge));

console.log(all);

// A basic `groupBy` implementation
function groupByProp(prop, xs) { 
  return xs.reduce(
    (groups, x) => {
      const k = x[prop];
      if (!groups[k]) groups[k] = [x];
      else groups[k].push(x);
      return groups;
    },
    {}
  );
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!