Javascript array to multi level Object -
i have array in javascript , need convert multi-level object. example: input :
var myarray = ["abc", "def", "ghi"] ;
output want :
var myobj = {abc:{ def: { ghi: {} } }};
what efficient way achieve javascript ?
you can use reduce()
, pass empty object accumulator.
var myarray = ["abc", "def", "ghi"] ; var obj = {} myarray.reduce((r, e) => r[e] = {}, obj); console.log(obj)
Comments
Post a Comment