Javascript Iteration Error - Variable Variable

Not an expert on 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 it makes sense what I want to do - is it possible

Basically make a variable / object by adding to it

Greetings

+3
source share
3 answers

One way to achieve this is eval()- - (usually a very bad idea)

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

Another, more complex, but relatively effective way would be to create a function that gives you the ability to dynamically change arbitrary hierarchies of objects at runtime. Here is 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 will look like this:

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

Here is 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 this, IMHO, is the easiest, but least extensible way:

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

JS.

+1

. , , "store" + .

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

In the browser, you can also use the namespace windowto declare your variables (avoiding use eval):

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

Source: https://habr.com/ru/post/1792998/


All Articles