I have following kind of ind开发者_StackOverflow中文版ex while 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
}
Use the array style property accessor:
data.rows[0]["student_"+i];
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
精彩评论