JSFiddle - React, Tailwind, and code Playground
by Darker
HTML
<pre id="out">
</pre>
JavaScript
const input = {
"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"
}
]
};
function createHiddenFields(obj) {
// ignore nulls
if(obj == null) {
return;
}
// For an array, just do the update for each object in array
else if(obj instanceof Array) {
for(const subObj of obj) {
createHiddenFields(subObj);
}
}
// For object, do the hidden field thing
else if(typeof obj == "object") {
// Replace all string values with hiddenVal
for(const valueName of Object.getOwnPropertyNames(obj)) {
// Do not override value nodes
if(valueName == "value") {
continue;
}
else {
// Process sub-objects
if(typeof obj[valueName] == "object") {
createHiddenFields(obj[valueName]);
}
// Replace primitive property values
else {
obj[valueName] = { hide: true };
}
}
}
// add hidden field for affects
if(typeof obj.affects == "object") {
obj.hide = true;
}
}
}
createHiddenFields(input);
console.log(input);
document.querySelector("#out").appendChild(new Text(JSON.stringify(input, null, " ")));