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

199
Vistas
How to get the sum of a key in an array of object and remove duplicates of consecutive objects?

I have a main array -

const arr = [
    {  description: 'Senior', amount: 50 },
    {  description: 'Senior', amount: 50 },
    {  description: 'Adult', amount: 75 },
    {  description: 'Adult', amount: 35 },
    {  description: 'Infant', amount: 25 },
    {  description: 'Senior', amount: 150 }
]

I want help with an es6 operation which will add the amount based on the key(description) and remove the duplicates.

Result array will somewhat look like -

const newArr = [
        {  description: 'Senior', amount: 120 },
        {  description: 'Adult', amount: 110 },
        {  description: 'Infant', amount: 25 },
        {  description: 'Senior', amount: 150 }
]

I have been using the reduce operator to achieve this using the solutions from below, but that removes the non-consecutive objects as well.

It would be really helpful if someone can help me with some es6 operators to perform the same operation.

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

0

const arr = [
  { description: "Senior", amount: 50 },
  { description: "Senior", amount: 50 },
  { description: "Adult", amount: 75 },
  { description: "Adult", amount: 35 },
  { description: "Infant", amount: 25 },
];

const result = Object.values(
  arr.reduce((acc, item) => {
    acc[item.description] = acc[item.description]
      ? { ...item, amount: item.amount + acc[item.description].amount }
      : item;
    return acc;
  }, {})
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

  • Using Array#reduce, iterate over the array while updating a Map where the key is the description and the value is the record with total amount
  • Using Map#values, get the list of grouped objects

const arr = [ { description: 'Senior', amount: 50 }, { description: 'Senior', amount: 50 }, { description: 'Adult', amount: 75 }, { description: 'Adult', amount: 35 }, { description: 'Infant', amount: 25 } ];

const res = [...
  arr.reduce((map, current) => {
    const { description } = current;
    const grouped = map.get(description);
    if(!grouped) {
      map.set(description, { ...current });
    } else {
      map.set(description, { ...grouped, amount: grouped.amount + current.amount })
    }
    return map;
  }, new Map)
  .values()
];

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You may just use this snippet

array.filter((item, index) => array.indexOf(item) === index);

Refer to this link for more details. Or just copy this solution.

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