Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!