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

87
Views
More efficient way to find max of multiple like-objects in array JS?

Is there an array method (or any other way really) to group objects within an array by a property and sum by another. Then return the max?

let array = [
    {ticker:'AAPL',val:400},
    {ticker:'IBM',val:200},
    {ticker:'AAPL',val:500},
    {ticker:'SBUX',val:800},
]

let obj = {}

array.forEach( h => {
    obj[h.ticker] = obj[h.ticker] || 0;
    obj[h.ticker] += h.val
})

let temp = Object.keys( obj ).map(function ( key ) { return obj[key] }); 

console.log(Math.max(...temp)) // returns 900

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

0

If you just want to modify it to use reduce instead of forEach, you can do:

let obj = array.reduce((a,b) => ({...a, [b.ticker]: (a[b.ticker] || 0) + b.val}), {});
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!