Currently, I am dealing with a set of 4 JSON objects that contain nested data within them. These objects are all stored in an array named classes. Below is an example showcasing the format of one of these class objects:
let class_A = {
professor: "Joey Smith",
numberStudents: 25,
courseCode: "COMS 2360",
seating: {
"FirstRow": {
0: {
firstName: "Sarah",
collegeMajor: "English",
},
1: {
firstName: "Bob",
collegeMajor: "Computer Engineering",
},
2: {
firstName: "Dylan",
collegeMajor: "Mathematics",
}
},
"SecondRow": {
3: {
firstName: "Molly",
collegeMajor: "Music"
}
}
}
};
I'm currently facing difficulties in accessing the final fields within each class object (firstName and collegeMajor). I am only able to reach the indexes under each row number.
let classes = [class_A, class_B, class_C, class_D];
let classesAvailable = document.getElementById('classes');
let selectedClass = classes[classesAvailable.value];
for(rowNum in selectedClass.seating){
for(index in selectedClass.seating[rowNum]){
console.log(index);
//console.log(selectedClass.seating[rowNum[index]].firstName);
}
}
In this example, when I use console.log(index), the output is as follows:
0
1
2
3
However, I am unable to display the first name and college major of each student in every row. Even though I attempted to apply a similar approach by using console.log(selectedClass.seating[rowNum[index]].firstName), it resulted in the following error:
Cannot read properties of undefined (reading 'firstName')
If anyone has insight into what could be causing issues with my process, your input would be greatly appreciated.