I wanna create a nested object dynamically. I can create it hard coded. Is it possible to do this with a loop ?
result = {}
keys = ["a", "b", "c", "d"]
result[keys[0]] = {}
result[keys[0]][keys[1]] = {}
result[keys[0]][keys[1]][keys[2]] = {}
result[keys[0]][keys[1]][keys[2]][keys[3]] = "cool"
I want to pass an integer for example if it is "3", this should created an object like:
result = {
"a": {
"b": {
"c": "cool"
}
}
}
If it is 4, :
result = {
"a": {
"b": {
"c": {
"d": "cool"
}
}
}
}
So on ...
edit:
I am also checking result object, in order to create this nested structure. If there is not any field yet, I simply create the object.
Using this structure to group data. Any chance to check these dynamically ?
if (!result[keys[0]])
if (!result[keys[0]][keys[1]])
if (!result[keys[0]][keys[1]][keys[2]])

keyscontain the correct number of input (what if the input is 5)?