I've been trying to make a recursive function based on the help I got here: looping through an object (tree) recursively
I want to go through the object o and create a new one that is keyed on each key, and has a value of parents for everything above it.
My brain doesn't do recursive well, take a crack at it :-) .. if you want to try with the real data it is here
function eachRecursive(obj) {
for (var k in obj) {
if (typeof obj[k] == "object" && obj[k] !== null ) {
eachRecursive(obj[k])
}
else {
// do something to add to codes
}
}
}
var o = {
"1": {
name: "hi 1",
children: {
"1.1": {
name: "hi 1.1",
children: {
"1.1.1": {
name: "hi 1.1.1",
children: {}
}
}
},
"1.2": {
name: "hi 1.2",
children: {}
}
}
},
"2": {
name: "hi 2",
children: {
"2.1": {
name: "hi 2.1",
children: {}
}
}
}
}
var codes = {}
eachRecursive(o)
console.log(codes)
// What I wish to have.
//{
// "1": {
// "name":"hi 1",
// "parents":[]
// },
// "2": {
// "name": "hi 2",
// "parents": []
// },
// "1.1": {
// "name": "hi 1.1",
// "parents": ["1"]
// },
// "1.1.1": {
// "name": "hi 1.1.1",
// "parents": ["1", "1.1"]
// },
// "1.2": {
// "name": "hi 1.2",
// "parents": ["1"]
// },
// "2.1": {
// "name": "hi 2.1",
// "parents": ["2"]
// }
//}