I'm learning OOP and am curious to discover why my code isn't looping through all the object instances and logging out the 'firstName' property - check out my demo below:
var people = {
"person1" : {
"firstName": "John"
, "lastName": "Smith"
, "age" : 25
, "checkedin" : true
},
"person2" : {
"firstName": "Nick"
, "lastName": "Debruyne"
, "age" : 24
, "checkedin" : false
}
}
// console.log(people); // works correctly
for (let person in people) {
// console.log(person); // works correctly
console.log(person.firstName); // returns undefined
}
Codepen: https://codepen.io/ns91/pen/VwZoegw?editors=1111
This code should just run through each object instance and log the firstName values of each object (being 'John' and 'Nick'). Is there some rookie error I'm doing here?
Also, why does 'undefined' show in the console? I've noticed this happens for other things I log in the console, and even when successful, it still occasionally occurs. Could someone explain the process of what's going on in the browser here, or why it's showing undefined?
And is this JSON or JS? I wonder if it needs to convert into JS to be readable in JS for loops?
Thanks for any advice here.