Vote count: 0
I am trying to display the tree given below in the format. Please find my solution and any changes that can help.
var loc1 = {
id: 2433,
name: "Sunnyvale",
parent: {
id: 3434,
name: "California",
parent: {
id: 4234,
name: "US"
}
}
}
var loc2 = {
id: 2433,
name: "Beijing",
parent: {
id: 3434,
name: "China"
}
}
var result1 = "";
var result2 = "";
function baz(o) {
for (var i in o) {
if (typeof(o[i])=="object") {
console.log(o[i].name)
baz(o[i]);
}
}
}
baz(loc1); //"US->California->Sunnyvale "
//baz(loc2); //"China->Beijing"
asked 38 secs ago
Traverse a JSON tree
Aucun commentaire:
Enregistrer un commentaire