I have javascript object like follows:
var school = {
grade1:[
{
"teacher":"anna",
"student":"jacky"
},
{..},...
]
grade2:[...]
}
I ran into a problem as I tried to retrieve content using a function
function getNames(grade){
$('#demo').text(school.grade[0].teacher); //returns an error
}
I could use school.grade1[0].teacher
but I want to have a grade
variable.
As I tried this and it obviously doesn't work:
var grade = 'grade1'; //I also tried just grade1 but it is an undefined varaible
getNames(grade);