Hi,
I’d appreciate some help with my homework.
I’ve got to display each student’s name and the results for each of their modules using vanilla JavaScript.
the following code displays the values I’m after, but I’d really like to know how to improve it.
var data = {
"students":[
{
"name":"Eric Smith",
"modules":{
"1":{
"id":"COMP40003",
"name":"Software Development",
"result":80
},
"2":{
"id":"COMP40001",
"name":"Digital technologies",
"result":71
},
"3":{
"id":"COMP40004",
"name":"Web Development",
"result":47
},
"4":{
"id":"COMP40002",
"name":"Networking Concepts and Cyber Security",
"result":0
}
}
},
{
"name":"Bob",
"modules":{
"1":{
"id":"COMP40003",
"name":"Software Development",
"result":80
},
"2":{
"id":"COMP40001",
"name":"Digital technologies",
"result":71
},
"3":{
"id":"COMP40004",
"name":"Web Development",
"result":47
},
"4":{
"id":"COMP40002",
"name":"Networking Concepts and Cyber Security",
"result":0
}
}
}
]
};
var json = JSON.parse(JSON.stringify(data));
for (var key in json["students"]) {
console.log(json["students"][key]["name"]);
console.log('-------------------');
for (var modkey in json["students"][key]["modules"]) {
console.log(json["students"][key]["modules"][modkey]["name"] + " " + json["students"][key]["modules"][modkey]["result"]);
console.log('-------------------');
};
};
Thanks