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

179
Vistas
How to reduce objects in an array based off an existing key value

So I have an existing array with three objects:

              [ { date: 3/12/2022, oService: 10}
                { date: 3/13/2022, oService: 12, aService: 1}
                { date: 3/13/2022, oService: 1 }]

Based on the date, I would like to get something that looks like the following:

                [ {date: 3/12/2022, oService: 10}
                  {date: 3/13/2022, oService: 13, aService: 1}]

additionally, I could have up to 10 services, so I cant reduce by prev.oService. I'll have an array of services that looks something like:

const Services = [aService, bService, cService, oService, yService]

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

0

I think it's best to think of the problem as 2 different steps:

  • First, get all things that belong to the same date together. This is a groupBy that you should be able to look up on StackOverflow or borrow from an existing library.
  • Then, you merge each of the date groups in to a single object.

The merge operation is a bit more custom than the groupBy operation. You can implement it in many ways. I would propose to:

  • Implement merge to deal with just 2 objects
  • Inside merge, loop over all unique keys in those objects and sum them (excluding the date key)
  • To apply merge to a date group, use reduce. Note that it's safe to not provide a seed argument here, because you are guaranteed to not have empty groups.

// Merge two date-service entries in to one
const merge = (a, b) => {
  const merged = Object.assign({}, a);
  
  Object.keys(b).forEach(k => {
    if (k !== "date")
      merged[k] = (merged[k] || 0) + b[k];
  });
  
  return merged;
};

const input = [ { date: "3/12/2022", oService: 10},
                { date: "3/13/2022", oService: 12, aService: 1},
                { date: "3/13/2022", oService: 1 }];
          
// Create groups that have a matching date
const byDate = groupByProp("date", input);

// Reduce each group to a single item by merging
const all = Object.values(byDate).map(xs => xs.reduce(merge));

console.log(all);

// A basic `groupBy` implementation
function groupByProp(prop, xs) { 
  return xs.reduce(
    (groups, x) => {
      const k = x[prop];
      if (!groups[k]) groups[k] = [x];
      else groups[k].push(x);
      return groups;
    },
    {}
  );
}

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