I have a specific goal in mind. My JSON data is multi-level, structured like this:
var json = {
'first_level': {
'second_level': ['one', 'two']
},
'another_first_level': true
}
and I want to be able to access and modify it in this manner:
load('first_level.second_level');
save('first_level.second_level', ['one', 'two', 'lol']);
Reading the data is straightforward:
function load(path) {
var arr = path.split('.');
var result = json;
arr.forEach(function(v, i){
result = result[arr[i]];
});
return result;
}
However, I am unsure how to update the JSON variable using the same string format, especially when the data is deeply nested.. It could be up to 10 levels deep.
Is this even possible?
Here is a codepen with the current example: http://codepen.io/ExClouds/pen/jWBrob?editors=001