Not sure how to word this.
I am trying to use a variable to determine how far to drill into an object to return a value.
var target = "level1.level2.var";
var myObject = {
level1: {
level2: {
var: 'value'
}
}
}
var returnVal = myObject.target;
How could this be done? Clearly this won't work. Is it possible some other way?
I figured I would have to maybe explode the target var and then loop for each level, but thought I'd ask to see if there was any easier way I could be overlooking.
Not sure how to word this.
I am trying to use a variable to determine how far to drill into an object to return a value.
var target = "level1.level2.var";
var myObject = {
level1: {
level2: {
var: 'value'
}
}
}
var returnVal = myObject.target;
How could this be done? Clearly this won't work. Is it possible some other way?
I figured I would have to maybe explode the target var and then loop for each level, but thought I'd ask to see if there was any easier way I could be overlooking.
Share Improve this question asked Dec 23, 2011 at 21:05 Justin CarlsonJustin Carlson 2,7102 gold badges20 silver badges19 bronze badges 6 | Show 1 more comment5 Answers
Reset to default 10You could use this function:
function get_property_from_target(obj, target){
var arr = target.split('.');
for(var i = 0; i < arr.length; i++){
if(obj)
obj = obj[arr[i]];
}
return obj;
}
Then call it like:
get_property_from_target(myObject, target);
I'd rename the function to something better too.
Also please don't name an objects property var
since that is a keyword in Javascript it can be confusing, and I'm not sure that it will always work the way you expect or if it will just cause errors in some browsers.
Easier? Sure, use eval
:
var obj = eval('myObject.' + target);
That's not really a serious answer, though. You shouldn't use eval
that way in good code. The only other way as far as I know, however, is to loop as you describe:
var items = target.split('.');
var obj = myObject;
var i;
for(i = 0; i < items.length; i++) {
obj = obj[items[i]];
}
Or, with a regular expression hack:
var obj = myObject;
target.replace(/[^\.]+/g, function(m) {
obj = obj[m];
});
Regardless, you can now use obj
.
I can see two ways of doing this:
Using
eval()
(frowned upon, aseval
isevil
):var returnVal = eval('myObject.' + target);
Using a loop:
var split = target.split('.'); var newTarget = myObject; for (var i = 0; i < split.length; i++) { newTarget = newTarget[split[i]]; }
there is no way, other than writing code to split your target on .
, then for each token, drill down into the object as long as the "path" is valid.
What I would do (maybe not the best, but it'll work), is what you've suggested. Explode the variable, and then go to that level.
var target = "level1.level2.var";
target = target.split('.');
var returnVal = myObject;
for(var i=0,len=target.length; i<len; i++){
returnVal = returnVal[target[i]];
}
console.log(returnVal); // value
returnVal = eval("myObject."+target)
? – Shiplu Mokaddim Commented Dec 23, 2011 at 21:10eval
. – gen_Eric Commented Dec 23, 2011 at 21:11