JavaScript doesn't have references to properties, which is effectively what you're trying to use there.
Instead, you can give yourself a method that will traverse an object and either retrieve or assign a property in it. Here's a quick and dirty in ES5 and earlier:
function accessPath(obj, path, value) {
var o = obj;
var i = 0;
var last = path.length - 1;
while (i < last) {
o = o[path[i]];
++i;
}
if (arguments.length < 3) {
// Getting
return o[path[last]];
} else {
// Setting
return o[path[last]] = value;
}
}
Live example:
function accessPath(obj, path, value) {
var o = obj;
var i = 0;
var last = path.length - 1;
while (i < last) {
o = o[path[i]];
++i;
}
if (arguments.length < 3) {
// Getting
return o[path[last]];
} else {
// Setting
return o[path[last]] = value;
}
}
var data = {
"id":"dfd",
"properties":{
"Pri":"2",
"Brief Description":"asdf",
"Description":"",
"tree":{
"var": "2",
"rav": "3"
}
}
}
var path = ['properties', 'tree', 'var'];
console.log("Existing: " + accessPath(data, path));
accessPath(data, path, "new value");
console.log("Updated: " + accessPath(data, path));
console.log("Confirm: " + data.properties.tree.var);
Looks fairly similar in ES2015+, other than perhaps how you check if value
is supplied.
Not pretty, but fairly efficient.
Actually, we can go further if we return an object with a getter and setter, which would look a bit like a property reference even though it isn't actually:
function makeAccessor(obj, path) {
var o = obj;
var i = 0;
var last = path.length - 1;
var lastName = path[last];
while (i < last) {
o = o[path[i]];
++i;
}
return {
get value() {
return o[lastName];
},
set value(value) {
o[lastName] = value;
}
};
}
Then, getting the accessor:
var accessor = makeAccessor(data, path);
And using it:
console.log(accessor.value);
accessor.value = "new value";
function makeAccessor(obj, path) {
var o = obj;
var i = 0;
var last = path.length - 1;
var lastName = path[last];
while (i < last) {
o = o[path[i]];
++i;
}
return {
get value() {
return o[lastName];
},
set value(value) {
o[lastName] = value;
}
};
}
var data = {
"id":"dfd",
"properties":{
"Pri":"2",
"Brief Description":"asdf",
"Description":"",
"tree":{
"var": "2",
"rav": "3"
}
}
}
var path = ['properties', 'tree', 'var'];
var accessor = makeAccessor(data, path);
console.log("Existing: " + accessor.value);
accessor.value = "new value";
console.log("Updated: " + accessor.value);
console.log("Confirm: " + data.properties.tree.var);