Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda