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

159
Views
How to group by with sum in javascript using the reduce method

I have the following array:

let array = [
  { date: '2022-06-29', cost: 3.874886 },
  { date: '2022-06-28', cost: 3.357631 },
  { date: '2022-06-28', cost: 92.24 },
  { date: '2022-06-28', cost: 221.47 },
  { date: '2022-06-28', cost: 164.8 },
  { date: '2022-06-29', cost: 263.58 },
  { date: '2022-06-29', cost: 30.19 },
  { date: '2022-06-29', cost: 3.72 }, 
  { date: '2022-11-29', cost: 33.19 },
  { date: '2022-11-01', cost: 31.72 }
]

I'm trying to use the .reduce method to get the sum of cost, grouped by date, which would look like this:

[
  {date: '2022-06-28', cost: 481.867631},
  {date: '2022-06-28', cost: 301.364886},
  {date: '2022-11-01', cost: 31.72},
  {date: '2022-11-29', cost: 33.19}
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can do something like this

const group = data => Object.values(data.reduce((res, {date, cost}) => {
   return {
     ...res,
     [date]: {date, cost: (res[date] || {cost:0}).cost + cost}
   }

}, {}))


const data = [
  { date: '2022-06-29', cost: 3.874886 },
  { date: '2022-06-28', cost: 3.357631 },
  { date: '2022-06-28', cost: 92.24 },
  { date: '2022-06-28', cost: 221.47 },
  { date: '2022-06-28', cost: 164.8 },
  { date: '2022-06-29', cost: 263.58 },
  { date: '2022-06-29', cost: 30.19 },
  { date: '2022-06-29', cost: 3.72 }, 
  { date: '2022-11-29', cost: 33.19 },
  { date: '2022-11-01', cost: 31.72 }
]

console.log(group(data))

about 4 years ago · Juan Pablo Isaza Report

0

let array = [
  { date: '2022-06-29', cost: 3.874886 },
  { date: '2022-06-28', cost: 3.357631 },
  { date: '2022-06-28', cost: 92.24 },
  { date: '2022-06-28', cost: 221.47 },
  { date: '2022-06-28', cost: 164.8 },
  { date: '2022-06-29', cost: 263.58 },
  { date: '2022-06-29', cost: 30.19 },
  { date: '2022-06-29', cost: 3.72 }, 
  { date: '2022-11-29', cost: 33.19 },
  { date: '2022-11-01', cost: 31.72 }
]




const result = array.reduce((acc, item) => {
  const itemIndex = acc.findIndex(i => i.date === item.date);
  if (itemIndex !== -1) {
    acc[itemIndex].cost += item.cost
  } else {
   acc.push(item)
  }
  return acc;
}, [])

console.log(result)
[
  {date: '2022-06-28', cost: 481.867631},
  {date: '2022-06-28', cost: 301.364886},
  {date: '2022-11-01', cost: 31.72},
  {date: '2022-11-29', cost: 33.19}
]

about 4 years ago · Juan Pablo Isaza Report

0

here is my way to achieve your desire output.

let array = [
  { date: '2022-06-29', cost: 3.874886 },
  { date: '2022-06-28', cost: 3.357631 },
  { date: '2022-06-28', cost: 92.24 },
  { date: '2022-06-28', cost: 221.47 },
  { date: '2022-06-28', cost: 164.8 },
  { date: '2022-06-29', cost: 263.58 },
  { date: '2022-06-29', cost: 30.19 },
  { date: '2022-06-29', cost: 3.72 }, 
  { date: '2022-11-29', cost: 33.19 },
  { date: '2022-11-01', cost: 31.72 }
]

const newArray = array.reduce((prev, curr) => {
  const check = prev.findIndex(f => f.date === curr.date);
  if (check > -1) {
    prev[check].cost += curr.cost
  } else {
    prev.push(curr)
  }
  return prev;
}, []);

console.log(newArray)

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!