My JSON:
const records = {
"record A": {
"name": "John",
"last name": "Doe",
},
"record B": {
"name": "Jane",
"last name": "Smith",
},
};
And here is my script:
const obj = {
logName: function(name) {
console.log(name)
},
displayNames: function(data) {
for (record in data) {
let name = record["name"]
this.logName(name)
}
},
};
The output:
undefined
undefined
When I use the following code snippets:
// first example
console.log(records["record A"]["name"])
// output -> John
// second example
for (record in records) {
console.log(record)
}
// output -> record A
// output -> record B
However, when I try to simplify my code like this:
for (record in records) {
console.log(record["name"])
}
// output -> undefined
// output -> undefined
How can I access nested JSON data correctly?