4

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.

6
  • returnVal = eval("myObject."+target) ? Commented Dec 23, 2011 at 21:10
  • @Shiplu: Yeah, but that uses eval. Commented Dec 23, 2011 at 21:11
  • As long as you are not passing user inputs there or check the string its safe. Commented Dec 23, 2011 at 21:13
  • possible duplicate of In javascript how can I dynamically get a nested property of an object Commented Dec 23, 2011 at 21:19
  • @Shiplu eval is also much slower than a simple split and loop Commented Dec 23, 2011 at 21:20

5 Answers 5

10

You 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Added if(obj) into the loop so that it will return undefined if you send in an invalid target, instead of throwing an error.
4

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.

2 Comments

/[^\.]+/g.exec('level1.level2.var'); returns ['level1']. Why not just use target.split('.')?
@Rocket: Yes, I was halfway into one solution :) I already edited it though, read again.
2

I can see two ways of doing this:

  • Using eval() (frowned upon, as eval is evil):

    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]];
    }
    

2 Comments

I'd suggest not using eval, but I guess it's one solution.
I'll add in that it is not optimal.
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.