开发者

Javascript Iteration issue - variable variable

Not an expert on the old JS so here goes

I have

store1.baseParams.competition = null;
store2.baseParams.competition = null;
store3.baseParams.competition = null;

What I want to do is

for (i=1; 1<=3; 1++) {
    store + i +.baseParams.competition = null;
}

Hope that makes sense what I want to do - is it possible

Bas开发者_StackOverflow社区ically make a variable / object by adding to it

Cheers


One way to accomplish this is via eval() - (usually a Very Bad Idea)

for (var i=1; i<=3; i++) {
    eval("store" + i + ".baseParams.competition = null;");
}

Another, more complex but relatively efficient way would be to create a function which gives you the ability to mutate arbitrarily deep object hierarchies dynamically at run-time. Here's one such function:

/*
   Usage: 
     Nested objects:
     nested_object_setter(object, ['property', 'propertyOfPreviousProperty'], someValue);
     Top-level objects:
     nested_object_setter(object, 'property', someValue);
 */

function dynamic_property_setter_base(obj, property, value, strict) {
   var shouldPerformMutation = !strict || (strict && obj.hasOwnProperty(property));
   if(shouldPerformMutation) {
      obj[property] = value;    
   }
   return value;
}

function dynamic_property_setter(obj, property, value) {
   return dynamic_property_setter_base(obj, property, value, false);
}

function nested_object_setter(obj, keys, value) {
    var isArray = function(o) {
      return Object.prototype.toString.call(o) === '[object Array]';
    };

    //Support nested keys.
    if(isArray(keys)) {
       if(keys.length === 1) {
          return nested_object_setter(obj, keys[0], value);
       }

       var o = obj[keys[0]];
       for(var i = 1, j = keys.length - 1; i < j; i++)
          o = o[keys[i]];
       return dynamic_property_setter(o, keys[keys.length - 1], value);
    }

    if(keys != null &&
       Object.prototype.toString.call(keys) === '[object String]' &&
       keys.length > 0) {
       return dynamic_property_setter(obj, keys, value);
    }

    return null;
}

Your code would look like this:

for(var i = 1; i <= 3; i++) 
  nested_object_setter(this, ['store' + i, 'baseParams', 'competition'], null);

Here's another example, running in the JS console:

> var x = {'y': {'a1': 'b'}};
> var i = 1;
> nested_object_setter(this, ['x','y','a' + i], "this is \"a\"");
> x.y.a1
"this is "a""

Another way to do it, IMHO this is the simplest but least extensible way:

this['store' + i].baseParams.competition = null;


That won't work. You can make an object though, storing the 'store'+i as a property.

 var storage = {},i=0;
 while(++i<4) {
    storage['store' + i] = { baseParams: { competition:null } };
 }
 Console.log(String(storage.store1.baseParams.competition)); //=> 'null'

In a browser, you can also use the window namespace to declare your variables (avoiding the use of eval):

var i=0;
 while(++i<4) {
    window['store' + i] = { baseParams: { competition:null } };
 }
 Console.log(String(store1.baseParams.competition)); //=> 'null'


for (i=1; i<=3; i++) {
    this["store" + i + ".baseParams.competition"] = null;
}

Just another form of assigning variables in JS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜