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

227
Views
How to group an javascript object?

I have a javascript array object from a database input like this:

 [{ date: '2021-09-15', type: 'cats', value: 4 },
  { date: '2021-09-15', type: 'dogs', value: 5 },
  { date: '2021-09-16', type: 'cats', value: 1 },
  { date: '2021-09-16', type: 'dogs', value: 4 }]

And I need an output like this:

 [{ date: '2021-09-15', cats: 4, dogs: 5 },
  { date: '2021-09-16', cats: 1, dogs: 4 }]

I am trying to use the reduce function by this way, however, I can't get it to work (it's grouping the data but not modifying it thus, they are left with a single value)

const data = [
  { date: '2021-09-15', type: 'cats', value: 4 },
  { date: '2021-09-15', type: 'dogs', value: 5 },
  { date: '2021-09-16', type: 'cats', value: 1 },
  { date: '2021-09-16', type: 'dogs', value: 4 }
]

const groups = data.reduce((groups, item) => {
  const key = item[item.date];
  if (!groups[key]) {
    groups[item.date] = []
  }

  if (item.type == "cats") {
    item.cats = item.value;
  } else if (item.type == "dogs") {
    item.dogs = item.value;
  }

  groups[item.date].push(item);

  return groups;
}, {});

console.log(groups)
.as-console-wrapper { max-height: 100% !important; }

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

0

If second object with same date and already existing type. This also covers the case where you have multiple objects with same type then it will add up the value

const arr = [
  { date: "2021-09-15", type: "cats", value: 4 },
  { date: "2021-09-15", type: "cats", value: 5 },
  { date: "2021-09-16", type: "cats", value: 1 },
  { date: "2021-09-16", type: "dogs", value: 4 },
];

const map = new Map();
arr.forEach((o) => {
  const { date, type, value } = o;
  if (!map.has(date)) map.set(o.date, { date, [type]: value });
  else {
    const target = map.get(date);
    const { type: newType, value: newValue } = o;
    target[newType] = target[newType] ? target[newType] + newValue : newValue;
  }
});

const result = [...map.values()];
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

This is really a two-step process...

  1. Collect the data grouped by date into a map
  2. Transform the final result into the array you want

const data = [{"date":"2021-09-15","type":"cats","value":4},{"date":"2021-09-15","type":"dogs","value":5},{"date":"2021-09-16","type":"cats","value":1},{"date":"2021-09-16","type":"dogs","value":4}]

const t1 = performance.now()

const mapped = data.reduce((map, { date, type, value }) => {
  // get the current entry for date, default to `{}`
  const obj = map.get(date) ?? {}
  
  // merge and set the new entry
  return map.set(date, {
    ...obj,
    [ type ]: (obj[type] ?? 0) + value
  })
}, new Map())

const groups = Array.from(mapped, ([ date, obj ]) => ({
  date,
  ...obj
}))

const t2 = performance.now()

console.log(groups, `\nTook ${t2 - t1}ms`)
.as-console-wrapper { max-height: 100% !important; }

It wasn't clear from your question but this answer sums up any same type values for the same date.

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!