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

163
Vistas
need to group objects, in array of objects, in one group

I have next data:

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

The task is to group objects by amount and percent, so that if amount and percent in different objects are the same I need to add their days. The result should look the next way:

const data = [
    {amount: 100, percent: 1, days: 17},
    {amount: 75, percent: 3, days: 11},
    {amount: 100, percent: 2, days: 5},
    {amount: 50, percent: 3, days: 9}
];

1st and 3rd objects are not grouped because of different percent. I have tried to use reduce method:

const result = data.reduce((groupedByAmount, current) => {
    let amount = current.amount;
    let percent= current.percent;
    if (amount == amount && percent == percent) {
        groupedByAmount.amount = amount;
        current.days += current.days
    }
    return groupedByAmount
}, {
    amount: 0,
    percent: null,
    days: 0
});

I don't how to check if amount and percent are the same, maybe map or filter would help

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

0

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

const doGroup = (data) => {
    const obj = data.reduce((result, next) => {
    const key = `${next.amount}_${next.percent}`
    const dd =  result[key]?.days ?? 0;
    result[key] = {...next, days: next.days + dd};
    return result;
  }, {});
  return Object.values(obj);
}

console.log(doGroup(data))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Considering each of the methods you've suggested:

  • reduce => reduce array elements to a single value. Can be used to create an object, then convert that object back to the array

  • map => return a value for every value in the array

  • filter=> return multiple (or single) matching values

You can loop through each value, building a new array and using filter() to find existing values to add to.

Note: this likely won't perform well with large arrays due to the multiple use of .filter() - using a composite key (combining two values into a single value) would likely perform better.

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

var result = [];

data.forEach(e => {
    var current = result.filter(d => d.amount == e.amount && d.percent == e.percent);
    if (current.length === 0) {
        result.push(e);
    } else {
        current[0].days += e.days;
    }
});
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

const temp = data.reduce((acc, {amount, percent, days}) => {
  acc[amount] ||= {}
  acc[amount][percent] ||= 0
  acc[amount][percent] += days
  return acc;
}
,{});

const result = Object.entries(temp).reduce((acc, [amount, obj]) => {
  acc.push(...Object.entries(obj).map(([percent, days]) => ({ amount: Number(amount), percent: Number(percent), days })))
  return acc
},[])

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