I possess a dynamically generated, multi-level nested object containing DISTINCT values. My goal is to flatten it (using either AngularJS or vanilla JS) and produce a straightforward array or object for each key/value pair. For example, if the object takes this form:
[
{name : "parent",
age: 21,
children: [
{ name : "child",
dob: [{
day: "01",
month: "01",
year : "82"
}],
children: [
{
name : "grandchild",
dob: [{
day: "01",
month: "01",
year : "02"
}]
}
]
}
]
}
];
The flattened object should resemble this structure:
"name":"parent",
"age":21,
"children.name":"child",
"children.dob.day":"01",
"children.dob.month":"01",
"children.dob.year":"82",
"children.children.name":"grandchild",
"children.children.dob.day":"01",
"children.children.dob.month":"01",
"children.children.dob.year":"02"
I have come across 2 functions to flatten an object, but both add indexes alongside every node. (0.children.0.children.0.dob.0.year) This does not serve my purpose, as my values are unique. I require the format specified above. You can view the functions in use within this codepen: https://codepen.io/anon/pen/qMXEmB
Is there anyone who can assist me in eliminating those bothersome "zeros" from my final object?