function getValueThroughPath(obj, keys) {
while(obj=obj[keys.shift()] || '', keys.length);
return obj || undefined;
}
function setValueThroughPath(obj, keys, val) {
while(keys.length>1) {
obj[keys[0]]= obj[keys[0]] || {};
obj= obj[keys.shift()];
};
obj[keys[0]]= val;
}
var object = {
key1: {
key2: {
key3: {
key4: "value"
}
}
}
}
setValueThroughPath(object, ['this', 'is', 'a', 'test'], 'Eureka!');
setValueThroughPath(object, ['this', 'is', 'a', 'new', 'test'], 'Hallelujah!');
console.log(getValueThroughPath(object, ['key1', 'key2', 'key3', 'key4'])); // "value"
console.log(getValueThroughPath(object, ['key1', 'key2', 'key5', 'key4'])); // undefined
console.log(getValueThroughPath(object, ['this', 'is', 'a', 'test'])); // "Eureka!"
console.log(getValueThroughPath(object, ['this', 'is', 'a', 'new', 'test'])); // "Hallelujah!"