Populate nested object from array?

Use reduce()

var myArr = ['foo', 'bar', 'baz'];
var myObj = {};

myArr.reduce(function(a, b) {
  a[b] = {};
  return a[b];
}, myObj);

console.log(myObj);

Leave a Comment