I am having trouble working with a JSON sample data. I have the following JSON object:
var classList =
[
{
'class1': {
'name': 'physics',
'year': '2015'
}
}, {
'class2': {
'name': 'geography',
'year': '2016'
}
}, {
'class3': {
'name': 'history',
'year': '2015'
}
}, {
'class4': {
'name': 'math',
'year': '2016'
}
}
]
I want to reiterate over each object of classList and get the 'name' value. I was trying something like:
for (i = 0; i < classList.length; i += 1) {
if (classList[i] !== undefined) {
returnVal = classList[i][0]['name'];
}
}
But, this doesn't seem to be working. The problem is the classList has further objects of different name 'class1', 'class2', 'class3' & 'class4'. I am not sure how to iterate over them. I don't want to hard code these values.
Thanks.