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

204
Views
find similar value of an array object and calculate the sold

Hi am trying to find similar value of this array object and calculate the some of sold for each same values

const sold = [
  { matri: 1, year: 2019, sold: 5 },
  { matri: 2, year: 2020, sold: 3 },
  { matri: 1, year: 2020, sold: 7 },
];

above i have one array object the expected output

{ matri: 1, year: 2020, sold: 5 },
{ matri: 1, year: 2020, sold: 7 },

sold: 5 + sold: 7

output sold : 12

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

0

You can use reduce to group and sum the sold property:

const sold = [
  { matri: 1, year: 2019, sold: 5 },
  { matri: 2, year: 2020, sold: 3 },
  { matri: 1, year: 2020, sold: 7 },
];

const result = Object.values(sold.reduce((carry, entry) => {
  if (!carry.hasOwnProperty(entry.matri)) {
     carry[entry.matri] = {matri: entry.matri, sold: entry.sold};
  }
  else {
     carry[entry.matri].sold += entry.sold;
  }
  return carry;
}, {}));

console.log(result);

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!