I have a variable
var chartData = [];
I have entered a few datas in that variable as inside a for loop :
for(i=0;i<31;i++)
var newDate = new Date(firstDate);
newDate.setDate(newDate.getDate() + i);
i++;
chartData.push({
date: newDate,
visits: i
});
The datas have entered in the array. but now i want to access those datas in a table again applying a for loop to make table rows. how to do that? I tried
document.write= " <tr> <td style='width:25%'>" + chartData.date[i] + " </td> <td style='width:25%'>" + chartData.visits[i] + "</td> <td style='width:25%'> " + chartData.visits[i]*i + " </td> <td style='width:25%'> " + chartData.visits[i]*chartData.visits[i] + " </td> </tr>";
But Couldnt get the datas... help needed. thankyou in advance..
Close ... try ...
chartData[i].date
chartData[i].visits
chartData[i].visits
chartData[i].visits*chartData[i].visits
The index is on chartData
, not in the object inside; as you add objects to the array, the index increments against chartData
.
chartData.date[i]
实际上应该是chartData[i].date
因为它的chartData
变量表示数组。
When you use push, push function sets the index automatically from zero. So your elements in tha array become like this:
chartData[0]={ date: newDate,visits: 0 }
chartData[1]={ date: newDate,visits: 1 }
so you can access elements from array:
chartData[your_index].date
chartData[your_index].visits
The syntax for accessing an Object value inside an array is
arrayname[index].Obj_Key
In your case, it should be
chartData[i].date
Here is a working DEMO for better understanding based on your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.