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

149
Vistas
How generate uniques grouping/summing one of the columns based on 2 criteria in Javascript?

I've been trying to get this one, but it's still hard for me.

The data:

let data = [
 [1, "Item A", "Food", 10],
 [2, "Item B", "Food", 5],
 [3, "Item C", "Food", 30],
 [4, "Item A", "Hygiene", 30],
 [4, "Item A", "Hygiene", 50],
 [6, "Item D", "Food", 7],
 [3, "Item C", "Food", 8],
 [1, "Item A", "Food", 60],
]

Result

let result = [
 [1, "Item A", "Food", 70],
 [2, "Item B", "Food", 5],
 [3, "Item C", "Food", 38],
 [4, "Item A", "Hygiene", 80],
 [6, "Item D", "Food", 7],
]

This is basic reduce() which I imagine would be the path to take, but how to set the criteria e generate unique rows?

const array1 = [1, 2, 3, 4];
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue, currentValue) => previousValue + currentValue,
  initialValue
);
console.log(sumWithInitial);

Appreciate your help!

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

0

I've used map with findIndex
I used also reverse(), Object.values with Object.fromEntries methods to get unique ones:

let data = [
 [1, "Item A", "Food", 10],
 [2, "Item B", "Food", 5],
 [3, "Item C", "Food", 30],
 [4, "Item A", "Hygiene", 30],
 [4, "Item A", "Hygiene", 50],
 [6, "Item D", "Food", 7],
 [3, "Item C", "Food", 8],
 [1, "Item A", "Food", 60],
]
let objs = data.slice(); //in order to not mutate the source array data
let sumObjs = objs.map((curr, i) => {
let indexPrev = objs.findIndex(
    (item) => item[1] == curr[1] && item[2] == curr[2]
);
if (indexPrev != -1 && i != indexPrev) {
    //i != indexPrev to not accumelate the current one
    //indexPrev =! -1 means that one is exist
    objs[indexPrev][3] += curr[3];
}
return curr;
});
let result = Object.values(
Object.fromEntries(
    sumObjs.reverse().map((obj) => [JSON.stringify(obj[1]+obj[2]), obj])
)
);
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could take a combined key for grouping.

const
    data = [[1, "Item A", "Food", 10], [2, "Item B", "Food", 5], [3, "Item C", "Food", 30], [4, "Item A", "Hygiene", 30], [4, "Item A", "Hygiene", 50], [6, "Item D", "Food", 7], [3, "Item C", "Food", 8], [1, "Item A", "Food", 60]],
    result = Object.values(data.reduce((r, a) => {
        const key = [0, 1].map(i => a[i]).join('|');
        if (r[key]) r[key][3] += a[3];
        else r[key] = [...a];
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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