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

240
Vistas
reduce an Object based on DATE and TIME + calculate a VALUE

I have an object and I want to reduce the date and time together and calculate a number.
At the moment I'm trying the following code (but the 'time' value is missing)...

const myObject = [
    {
        "date": "2023-01-01",
        "time": "18:00",
        "somevalue": 2
    },
    {
        "date": "2023-01-01",
        "time": "18:00",
        "somevalue": 5
    },
    {
        "date": "2023-01-02",
        "time": "19:00",
        "somevalue": 10
    },
    {
        "date": "2023-01-03",
        "time": "06:00",
        "somevalue": 13
    }
];

const all = myObject.reduce( (prev, value) => (
  { ...prev, [value.date]: (prev[value.date] || 0) + value.somevalue } ), {});

// to array
console.log(
  Object.entries(all).map( ([key, value]) => ( {date: key, somevalue: value} ) )
);

My question is: How can I add the "time" value. Like this:

[
    {
        "date": "2023-01-01",
        "time": "18:00",
        "somevalue": 7
    },
    {
        "date": "2023-01-02",
        "time": "19:00",
        "somevalue": 10
    },
    {
        "date": "2023-01-03",
        "time": "06:00",
        "somevalue": 13
    }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Maybe something like this?

const myObject = [ { date: "2023-01-01", time: "18:00", somevalue: 2 }, { date: "2023-01-01", time: "18:00", somevalue: 5 }, { date: "2023-01-02", time: "19:00", somevalue: 10 }, { date: "2023-01-03", time: "06:00", somevalue: 13 }];

const output = myObject.reduce((arr, acc) => {
  const index = arr.findIndex(({ date, time }) => date === acc.date && time === acc.time); //find duplicated elements by date
  if (index > -1) { // if you have duplicated element
    arr[index] = {
      ...arr[index], // merge objects
      somevalue: arr[index].somevalue + acc.somevalue // add value
    };
  } else {
    arr = [...arr, acc]; //if no duplicates values, just add to array. 
  }
  return arr;
}, []);

console.log(output)

I use reduce to merge duplicated elements by "date" and add the someValue.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This would also work

const myObject = [ { date: "2023-01-01", time: "18:00", somevalue: 2, }, { date: "2023-01-01", time: "18:00", somevalue: 5, }, { date: "2023-01-02", time: "19:00", somevalue: 10, }, { date: "2023-01-03", time: "06:00", somevalue: 13, }, ];

const output = Object.entries(
  myObject.reduce((prev, { date, time, somevalue }) => {
    // create a key like <date>$<time> and add up 
    // somevalue when key is discovered again
    prev[`${date}$${time}`] =
      prev[`${date}$${time}`] != undefined
        ? prev[`${date}$${time}`] + somevalue
        : somevalue;
    return prev;
  }, {})
).map(([key, somevalue]) => {
  // split the key using $ and get date and time 
  // return created object
  const [date, time] = key.split("$");
  return { date, time, somevalue };
});

console.log(output);

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