I have a question, how to create nested object from array? I.e. I have a following array:
var myArr = ['foo', 'bar', 'baz']
and need to make it an object like:
myObj = {foo: { bar: { baz: { } } }}
How can I do that properly?
I have a question, how to create nested object from array? I.e. I have a following array:
var myArr = ['foo', 'bar', 'baz']
and need to make it an object like:
myObj = {foo: { bar: { baz: { } } }}
How can I do that properly?
Use reduce()
var myArr = ['foo', 'bar', 'baz'];
var myObj = {};
myArr.reduce(function(a, b) {
a[b] = {};
return a[b];
}, myObj);
console.log(myObj);