Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda