Automating access to alphanumeric index in json

I have the following type of index when accessing json data using jquery

data.rows[0].student_1
data.rows[0].student_2
data.rows[0].student_3 and so on...

      

Now I want to automate this thing in a loop like

for(var i=1;i<length;i++)
{
  // so that i can access all student records i.e. student_1, student_2 and so on
 data.rows[0].student_+i; // this doesn't work

}

      

+2


a source to share


2 answers


You can use square brackets:

for(var i=1;i<length;i++)
{
 data.rows[0]['student_'+i];
}

      



see also here: http://24ways.org/2005/dont-be-eval

0


a source


Use Array property properties property:



data.rows[0]["student_"+i];

      

+1


a source







All Articles