1

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..

1
  • You are increasing i twice in the loop. Was that indended? Commented Jan 2, 2015 at 15:41

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

chartData.date[i] should actually be chartData[i].date because its the chartData variable that represents the array.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.