Given input:
let input = [
{
schema: ["user", "email"],
value: "test@test.com"
},
{
schema: ["user", "name", "firstName"],
value: "John"
},
];
let output = {};
for (let i = 0; i < input.length; i++) {
let data = input[i];
for (let j = 0; j < data.schema.length; j++) {
let s = data.schema[j];
// How to set nested levels here ? eg: output["user"]["email"] = {};
}
}
Expected output:
{
"user": {
"email": "test@test.com",
"name": {
"firstName": "John"
}
}
}
Here "schema" represents the nested structure in JSON for the data.
What is the approach for dynamically setting nested attributes using Javascript ?