Javascript Date Loop [closed]

Solution for Javascript Date Loop [closed]
is Given Below:

I wonder if someone can help me solve an issue with Javascript.

I have some date code in which I define a start and end date, and then run a loop between those dates and add each date to an array, my code looks like;

var start_date = new Date("2021-07-01");
var end_date = new Date("2021-07-31");

var date_data = {};

var loop_date = new Date(start_date);

while(loop_date <= end_date)
{
   format_date = loop_date.toISOString().split('T')[0];
   date_data[format_date]; 
   var newDate = loop_date.setDate(loop_date.getDate() + 1);
   loop_date = new Date(newDate);
}

This produces me an array of dates under the variable “date_data” and works great.

I then have a set of data from a CSV that I load into the page, the CSV date looks something like

2021-05-22,OK,41,DONE
2021-06-12,OK,12,DONE
2021-07-03,OK,67,DONE
2021-07-24,OK,09,DONE
2021-08-14,NO,0,NOTDONE

The numeric values 12, 67 and 09 are whats important in this CSV (rows 2 – 4)

I then load the CSV data into a variable called “workload”, and you will notice this data comes with dates set every 3 weeks apart.

What I want to do is run that date loop code and set a value from that CSV to each of the dates in the loop, so the final output of the code would look like

2021-07-01 = '12';
2021-07-02 = '12';
2021-07-03 = '67';
2021-07-04 = '67';
2021-07-05 = '67';
....
2021-07-21 = '67';
2021-07-22 = '67';
2021-07-23 = '67';
2021-07-24 = '09';
2021-07-25 = '09';
2021-07-26 = '09';
2021-07-27 = '09';
2021-07-28 = '09';
2021-07-29 = '09';
2021-07-30 = '09';

You will notice it sets the value from the CSV to the correct date in the array.

Then inside the loop I can simply do something like

date_data[format_date] = DATE_FROM_ARRAY;

Can anyone help me with this?

Thanks

I’d do it in 3 steps

Note: No tests for the start and end date actually present in the generated list

const aDay = 24*60*60*1000;
// step 1, parse the CSV
const states = `2021-05-22,OK,41,DONE
2021-06-12,OK,12,DONE
2021-07-03,OK,67,DONE
2021-07-24,OK,09,DONE
2021-08-14,NO,0,NOTDONE`.split("n").reduce((acc,line,cnt) => {
  const [date,status,num,doneornot] = line.split(",");
  acc[date] = num
  return acc
},{})

console.log(states);

// Step 2, generate the list of possible dates and values
const entryArr = Object.entries(states)
const datesVal = [];
for (let i=0;i<entryArr.length-1;i++) {
  const startDate = new Date(entryArr[i][0])
  const endDate = new Date(entryArr[i+1][0])
  const val = entryArr[i][1];
  for (let d = startDate.getTime(), n = endDate.getTime()-aDay;d<n;d+=aDay) {
    datesVal.push(`${new Date(d).toISOString().split('T')[0]}=${val}`)
  }
}

console.log(datesVal);

// Step 3, slice from start to end
const start_date = "2021-07-01";
const end_date = "2021-07-31";
const dateStart = datesVal.findIndex(item => item.startsWith(start_date));
const dateEnd = datesVal.findIndex(item => item.startsWith(end_date))

const dateList = datesVal.slice(dateStart,dateEnd+1);
console.log(dateList)