Consider the object:
{
"status": {
"affects": {
"date_of_death": "date_of_death",
"cause_of_death": {
"affects": {
"other_cause_of_death": "other_cause_of_death"
},
"value": "Other"
}
},
"value": "Deceased"
}
}
What I want to do is loop over this and add {hide: true}
. But there are some rules:
- If the key does not have object as a value, take the value and make it into
{hide: true}
- If it has
affects
, like cause of death does, addhide: true
after value.
So the resulting object should be:
{
"status": {
"affects": {
"date_of_death": {hide: true},
"cause_of_death": {
"affects": {
"other_cause_of_death": {hide: true}
},
"value": "Other",
"hide": true,
}
},
"value": "Deceased"
}
}
now this might seem easy, until you get something like this:
{
"comorbidities": [
{
"affects": {
"malignancy_type": {
"affects": {
"malignancy_type_other": "malignancy_type_other"
},
"value": "Other"
}
},
"value": "malignancy"
},
{
"affects": {
"heritable_syndrome_type": "heritable_syndrome_type"
},
"value": "heritable syndrome"
}
]
}
The exact same thing should happen here. Accept notice how its an array, This should do it recursively, deep diving into the affects, until no more can be found.
I have gotten this far:
export const createHiddenFields = (dependencies) => {
let hiddenFields = {};
if (dependencies === null) {
return hiddenFields;
}
for (const key in dependencies) {
if (dependencies[key].hasOwnProperty('affects')) {
hiddenFields[key] =
} else {
}
}
return hiddenFields;
}
But I am lost as to how to finish this. I know I want it recursive, but I also want it fast, but still legible.
Any Ideas?