Need help with transforming a recursive json object into another json object.
My input object looks like this:
{
person:
{
name: 'John Henry',
age: 63
},
children:
[
{
person:
{
name: 'Paul Henry',
age: 40
},
children:
[
{
person:
{
name: 'Tom Henry',
age: 10
}
}
{
person:
{
name: 'Mike Henry',
age: 12
}
}
]
},
{
person:
{
name: 'Wilson Henry',
age: 30
}
},
{
person:
{
name: 'Richard Henry',
age: 59
}
}
]
}
The output I want is:
[{
name: 'John Henry',
attributes: { age: 63 },
children:
[
{
name: 'Paul Henry',
attributes: { age: 40 },
children: [
{
name: 'Tom Henry',
attributes: { age: 10 }
},
{
name: 'Mike Henry',
attributes: { age: 12 }
}
]
},
{
name: 'Wilson Henry',
attributes: { age: 30 }
},
{
name: 'Richard Henry',
attributes: { age: 59 }
}
]
}];
This is what I have tried so far, but got stuck at the recursion part. Not sure how to bring everything together.:
var tree = {};
var getFamilyTree = function (input) {
tree.name = input.person.name;
tree.attributes = { 'age': input.person.age };
tree.children = [];
input.children.forEach(function (child) {
//not sure how to recursively go through the child nodes.
});
};