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 like
{
a:{
b:{
c:{
item:'Name'
}
}
}
}
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.
reverse().reduce( ... )
doneed a JSON string and simply didn't format the question correctly, just take one of the answers below, and doJSON.stringify( result )to get a JSON representation of that object