Suppose I have an array which tells me the list of labels and a value that I need to add them into a JSON object. How do you transform it so?
So basically I have is an array and a value 100
arr = ["1", "Male"]
Should be transform it into a object as so.
obj = {
"1":{
"Male":100
}
}
You could use
reduceRight
for this. Usevalue
as theinitialValue
of reduce and create a new level of nesting in every loop.You could save the last property and reduce the object. At the end assign the value to the last property.
This solution respects already given properties.