0

I'm using push function in javascript.

var chartData = [];
for(var i=0; i<3; i++) {

    chartData.push({

        date: new Date(year_s,mon_s,date_s,hr_s,min_s,sec_s),
        visits: chartData1[selection[i]][j].value,
        customBullet: show_annotations,
        balloonTextField: "testtesttest"
    });

}

I need to pass a dynamic variable like visits_1, visits_2, etc,. in place of visits variable in the above code. I have tried visits+"_"+i. But it is not working. Please help me to achieve this. Thanks in advance.

2
  • 1
    Note that there is no JSON in the code shown (JS objects and JSON are different things). Commented Feb 20, 2013 at 10:40
  • And this isn't really a problem of variable but of property. Commented Feb 20, 2013 at 10:44

1 Answer 1

4

You have to make it in two steps :

var chartData = [];
for(var i=0; i<3; i++) {
   // 1. create the object
   var d =  { 
      date: new Date(year_s,mon_s,date_s,hr_s,min_s,sec_s),
      customBullet: show_annotations,
      balloonTextField: "testtesttest"
   };
   // 2. then assign the visits_i property
   d['visits_'+i] = chartData1[selection[i]][j].value; 
   chartData.push(d);
}
Sign up to request clarification or add additional context in comments.

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.