I have an object with a triple nested objects. I want to flatten the object to clearly review the values and place in an HTML.
{
"templateName": "Test template",
"assignmentGroup": "Test template",
"fields": [{
"type": "Multi Select dropdown",
"entry": [{
"checked": false,
"value": "govtBusinses",
"displayValue": "Government Business Division"
}, {
"checked": true,
"value": "privateSector",
"displayValue": "Private Sector"
}, {
"checked": true,
"value": "publicSector",
"displayValue": "Public Sector"
}]
}, {
"type": "Text Field",
"entry": null
}]
}
I tried flatting it, but I want it to be in desired format.
flattenObject = function(ob) {
let toReturn = {};
for (let i in ob) {
if (!ob.hasOwnProperty(i)) {
continue;
}
if ((typeof ob[i]) === "object") {
let flatObject = this.flattenObject(ob[i]);
for (let x in flatObject) {
if (!flatObject.hasOwnProperty(x)) {
continue;
}
toReturn[i + "." + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
console.log(toReturn);
return toReturn;
};
Expected:
TemplateName : "Test template"
Assignment Group : "sample"
Field 0 :
Type : Multiselect dropdown
Entry 0 :
checked : false
value :
buisness:
Entry 1:
checked : false
value :
buisness:
Field 1:
Type:
.......
How can i achieve this?