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

151
Vistas
Sum up value on hour in javascript array

I have an array with values per date but would like to sum up all values on hour instead of separate date/hour. I guess I could iterate over it and remove yyyy-mm-dd and then sum up on duplicate hour object but is there a smarter way?

  {
    "series": [
      {
        "value": 1,
        "category": "2021-03-04T04:00:00.000"
      },
      {
        "value": 2,
        "category": "2021-03-04T05:00:00.000"
      },
      {
        "value": 3,
        "category": "2021-03-04T06:00:00.000"
      },
      {
        "value": 1,
        "category": "2021-03-05T04:00:00.000"
      },
      {
        "value": 2,
        "category": "2021-03-05T05:00:00.000"
      },
      {
        "value": 3,
        "category": "2021-03-05T06:00:00.000"
      }
    ]
  }
]

So the array below should only be:

[
  {
    "series": [
      {
        "value": 2,
        "category": "04:00:00.000"
      },
      {
        "value": 4,
        "category": "05:00:00.000"
      },
      {
        "value": 6,
        "category": "06:00:00.000"
      }
    ]
  }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Map and reduce - you can split in the reduce but this is more readable

const data = {
  "series": [
  { "value": 1, "category": "2021-03-04T04:00:00.000" },
  { "value": 2, "category": "2021-03-04T05:00:00.000" },
  { "value": 3, "category": "2021-03-04T06:00:00.000" },
  { "value": 1, "category": "2021-03-05T04:00:00.000" },
  { "value": 2, "category": "2021-03-05T05:00:00.000" },
  { "value": 3, "category": "2021-03-05T06:00:00.000" }
  ]
}
const newData = data.series
  .map(item => ({value:item.value, category: item.category.split("T")[1]})) // cut the date off
  .reduce((acc,cur) => { 
    const item = acc.find(({category}) => category === cur.category); // find if we already have the time in the array we are creating
    if (item) item.value += cur.value; // if yes, add the value
    else acc.push(cur); // else push the current object
    return acc;
  },
  [])
console.log(newData)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Array.reduce

Logic

  • Loop through input with Array.reduce.
  • Get the time node from each category by splitting the sting on T.
  • Check if a node with same time exist in accumulator.
  • If yes, add the current value to the value of existing node.
  • Or else push a new node with value and category.

const data = {
  series: [
    { value: 1, category: "2021-03-04T04:00:00.000" },
    { value: 2, category: "2021-03-04T05:00:00.000" },
    { value: 3, category: "2021-03-04T06:00:00.000" },
    { value: 1, category: "2021-03-05T04:00:00.000" },
    { value: 2, category: "2021-03-05T05:00:00.000" },
    { value: 3, category: "2021-03-05T06:00:00.000" },
  ],
};
const result = data.series.reduce((acc, curr) => {
  const time = curr.category.split("T")[1];
  const accNode = acc.find((node) => node.category === time);
  if (accNode) {
    accNode.value += curr.value;
  } else {
    acc.push({ value: curr.value, category: time });
  }
  return acc;
}, []);
console.log({
  series: 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