I need to create dynamically an object with nested objects from a string of like 'a.b.c.d' with a result as:
/*
var a = {
b: {
c: {
d: {}
}
}
};
//or:
var result = {
a: {
b: {
c: {
d: {}
}
}
}
}*/
Currently with my following script I am not able nest the objects.
What is wrong? could you provide me a solution? Thanks!
var options = 'a.b.c.d';
var parts = options.split('.');
var obj = {};
var addNestedProp = function(obj, prop) {
obj[prop] = {};
};
parts.forEach(function(part) {
addNestedProp(obj, part);
}, this);
console.log(obj);
/*
result wanted:
var a = {
b: {
c: {
d: {}
}
}
};
or:
var result = {
a: {
b: {
c: {
d: {}
}
}
}
}*/