I was working on a project where i am required to go through an array of objects after fetching the data, manipulate them and then save those that fulfill certain conditions. Everything else was working but, the storing of the date in the object.
so basically, the code is like this (dateAdd taken from https://stackoverflow.com/a/1214753/12316962):
var baddata=[]
fetch(fetch_url)
.then((response) => {
if (response.ok){
response.json()
.then(data => works = data.workOpportunities)
.then(() => {
for (let i = 0; i < data.length; i++)
{
data[i]['rooms']=0
data[i]['ontime']=0
data[i]['ontime']=dateAdd(new Date(time), 'second', Math.round(data[i].distance.value/50*60*60))
}
for (let i = 0; i < works.length; i++){
if(/*checking ontime and other conditions*/){
//do something
}
//do not do anything and push the data to baddata array
}
}
)
}
data[i]['ontime'] is showing 'invalid date{}', and so the if statements are failing. originally, i had only one loop for the whole thing, it was the same.
when i run the data[0]['ontime']=dateAdd(new Date(time), 'second', Math.round(data[0].distance.value/50*60*60)) it works fine outside the loop.
Where is the mess-up?
Aucun commentaire:
Enregistrer un commentaire