3

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

Basically make a variable / object by adding to it

Cheers

0

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Do you really need nested_object_setter in you example? I would say: var x = {'y': {'a': 'b'}}; x.y.a = 'this is \'a\''; is sufficient here.
@KooiInc This was just to demonstrate that the function works. Of course you wouldn't actually use it if you didn't have to dynamically compute one or more of the keys in the key path.
1

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'

Comments

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

Just another form of assigning variables in JS.

3 Comments

@Jacob - Does this still have to go through eval? I am getting confused.
typo: i=1; 1<=3; 1++ => 1 should b i
@SachinShanbhag I'm pretty sure that the dot syntax in that case doesn't work properly - you need to use subpockets.

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.