How to point to an object inside an object and display it without knowing the name.
I just want to display the values of the objects inside person.
using the if
I only want to display the values of the object
.
But how should I reference it to display it?
const person = {
name: "Jack",
family: "Miller",
age: 27,
cars: {
car1: "Bens",
car2: "Lexus"
}
}
for (let x in person) {
if (typeof person[x] === "object") {
// for (let x in ??){document.write(?? + ", ")}
} else {
document.write("_" + "<br>")
}
}
Expected output:
_
_
_
Bens, Lexus
You are almost there, just use Object.values()
:
const person = { name:"Jack", family:"Miller", age: 27, cars:{car1:"Bens", car2:"Lexus"}}
for (let x in person){
if(typeof person[x] === "object"){
document.write(Object.values(person[x]).join(', ') + "<br>");
}else{
document.write("_" + "<br>")
}
}