(I’m aware I might be able to use a for in loop
, but I’d prefer to use a for loop
for this)
Hey, I’m sorry if this is a dumb question, I’m still learning my way through JavaScript by challenging myself a bit more. I’ve spent all day with this issue but I can’t seem to figure it out and need some help.
I have the following JavaScript code for my object:
let student_information = {
student1: {
name: "John",
school: "School1"
},
student2: {
name: "Jane",
school: "School2"
}
};
I’m using the following for loop:
for(let i = 0; i < Object.keys(student_information).length; i++) {
console.log(Object.keys(student_information)[i]);
}
I know that this just simply outputs student1 and student2. But I want to access the nested objects inside each of the keys.
For instance, how would I grab the school name for student1 by running it through the loop?
I know I can do student_information.student1.school
, but I need it to run through a loop and achieve it this way.
Is my approach wrong? How can I achieve my desired outcome?
I’d really appreciate if anyone could help me out!