最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript dot notation with variable - Stack Overflow

programmeradmin6浏览0评论

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
  • returnVal = eval("myObject."+target) ? – Shiplu Mokaddim Commented Dec 23, 2011 at 21:10
  • @Shiplu: Yeah, but that uses eval. – gen_Eric Commented Dec 23, 2011 at 21:11
  • As long as you are not passing user inputs there or check the string its safe. – Shiplu Mokaddim Commented Dec 23, 2011 at 21:13
  • possible duplicate of In javascript how can I dynamically get a nested property of an object – Felix Kling Commented Dec 23, 2011 at 21:19
  • @Shiplu eval is also much slower than a simple split and loop – Paul Commented Dec 23, 2011 at 21:20
 |  Show 1 more comment

5 Answers 5

Reset to default 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.

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

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
发布评论

评论列表(0)

  1. 暂无评论