I have an array in java script .. something like:
var myarray = [\'a\',\'b\',c\'];
and var item = \'Name\';
and I want to convert that to something lik
var obj = {};
var curobj = obj;
for (var i = 0; i < myarray.length; i++) {
var newObj = {};
newObj[myarray[i]] = newObj;
curObj = newObj;
}
curObj.item = item;
The result you want will be in the obj
object.
var result = myarray.reverse().reduce(function (value, key) {
var result = {};
result[key] = value;
return result;
}, { item : item });
In other words, you're packing the result layer by layer into new objects, using your keys from the array.