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

159
Vistas
how to Sum the max from two keys similarly, object javascript

I need the sum total of the object, where, but the maximum of each similar pair javascript

var score = {
    "work_1": 5,
    "work_1|pm": 10,
    "work_2": 8,
    "work_2|pm": 7
    "work_3": 10
};

the work_3 doesn't have a pair similar I wish this result

total = 28

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Group the maximums by key-prefix in a new object, and then sum the values of that object. In both phases you can use reduce:

var score = {
    "work_1": 5,
    "work_1|pm": 10,
    "work_2": 8,
    "work_2|pm": 7,
    "work_3": 10
};

var result = Object.values(
    Object.entries(score).reduce((acc, [k, v]) => {
        k = k.split("|")[0];
        acc[k] = Math.max(v, acc[k] ?? v);
        return acc;
    }, {})
).reduce((a, b) => a + b, 0);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can get the property values of the object with Object.values, sort the resulting array, then sum the first 2 items:

var score = {
    "work_1": 5,
    "work_1|pm": 10,
    "work_2": 8,
    "work_2|pm": 7,
    "work_3": 10
};

const max2 = Object.values(score).sort().splice(0, 2)

const result = max2[0] + max2[1];
console.log(result)

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