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

132
Views
Need function to add increasing values of Dates

im looking for a function to add the value of the previous day to each next day.

I have following array:

{x: "8.9.2021", y: 0},
{x: "9.9.2021", y: 33},
{x: "10.9.2021", y: 0},
{x: "11.9.2021", y: 0},
{x: "12.9.2021", y: 0},
{x: "13.9.2021", y: 0},
{x: "14.9.2021", y: 0},
{x: "15.9.2021", y: 8},
{x: "16.9.2021", y: 0},
{x: "17.9.2021", y: 99},
{x: "18.9.2021", y: 0},
{x: "19.9.2021", y: 0},
{x: "20.9.2021", y: 113},
{x: "21.9.2021", y: 57},
{x: "22.9.2021", y: 16},
{x: "23.9.2021", y: 0},
...

And im looking for something like this:

{x: "8.9.2021", y: 0},
{x: "9.9.2021", y: 33},
{x: "10.9.2021", y: 33},
{x: "11.9.2021", y: 33},
{x: "12.9.2021", y: 33},
{x: "13.9.2021", y: 33},
{x: "14.9.2021", y: 33},
{x: "15.9.2021", y: 41},
{x: "16.9.2021", y: 41},
{x: "17.9.2021", y: 140},
{x: "18.9.2021", y: 140},
{x: "19.9.2021", y: 140},
{x: "20.9.2021", y: 253},
{x: "21.9.2021", y: 310},
{x: "22.9.2021", y: 326},
{x: "23.9.2021", y: 326},
...

That is what i tried but that froze my page

const add = (array) => { 
  let newArray= orders; 
  for (let i = 0; i < array.length; i++) { 
      newArray[i] = { x: orders[i].x, y: orders[i].y + orders[--i] && orders[--i].y, }; } 
   return newArray; 
  };

Didn't find any solutions yet. The solution should be as simple as possible.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can iterate over the array, and add the values of y

const data = [{x: "8.9.2021", y: 0},
{x: "9.9.2021", y: 33},
{x: "10.9.2021", y: 0},
{x: "11.9.2021", y: 0},
{x: "12.9.2021", y: 0},
{x: "13.9.2021", y: 0},
{x: "14.9.2021", y: 0},
{x: "15.9.2021", y: 8},
{x: "16.9.2021", y: 0},
{x: "17.9.2021", y: 99},
{x: "18.9.2021", y: 0},
{x: "19.9.2021", y: 0},
{x: "20.9.2021", y: 113},
{x: "21.9.2021", y: 57},
{x: "22.9.2021", y: 16},
{x: "23.9.2021", y: 0}]

const incremented = []
data.forEach((el, index) => {
  index === 0 ? incremented.push(el) : 
  incremented.push({...el, y: el.y + incremented[index-1].y})
})

console.log(incremented)

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!