I want to be able to create subkeys if needed when assigning values to an object.
The thing is I get a JSON that can be having these values:
{
"myKey": "Foobar",
"myOtherKey.mySubkey.myOtherSubkey": "Bazboo"
}
Saying we want to inject the values to Node's global object, we would have the following:
global: {
myKey: "Foobar",
myOtherKey: {
mySubkey: {
myOtherSubkey: "Bazboo"
}
}
}
What I have at the moment is this :
function addGlobal (key, val) {
if(~key.indexOf('.')) {
let keys = key.split('.');
let context = global;
while(keys.length) {
key = keys.shift();
if(! (key in context)) {
context[key] = {};
}
context = context[key]
}
context = val;
}
else {
global[key] = val;
}
}