2

I have multiple variables named for instance "s11" and "s12" etc.

var s11 = 0;
var s12 = 0;

In addition to that, I also have an array containing all the names of these variables.

var wA = new Array("s11", "s12");

The values in the array are added automatically in the script later on, depending on user activity.

My problem is that I would like for example the variable "s11" to ++ every time it occurs as a value in the array. Can this be done?

3
  • Can you use an array instead of the "multiple variables named s11 s12"? Commented Jun 30, 2013 at 16:50
  • Yes, but you'd probably have to write an if statement for every variable that could possibly be incremented. Could you make a second array thats indexed with the field names and increment that instead? eg var fields = {"s11":0,"s12":0}; Commented Jun 30, 2013 at 16:51
  • You mean I can simply input the count into the wA array? Then I would not need the variables. Commented Jun 30, 2013 at 17:10

5 Answers 5

1

Using variables, will make your life hard. Here is one approach that does both. Use associative array, like this:

var count = {};
var wA = new Array("s11", "s12");


function updateArray(value) {

  wA.push(value);

  if(! (value in count) ) {
    count[value] = 0;
  }
  count[value]++;
}

Then use it like:

updateArray("s11");
updateArray("s12");

Now count will look like: {s11: 1, s12: 1} & wA will look like: ['s11', 's12']

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

3 Comments

Seems to be working perfectly! I will just double check everything before pressing solved.
@Thomas: yea, also don't forget to +1 ;-)
Yes, it works as beautifully as a sunset on the eastern archipelago of Sweden, with a soft breeze running through your hair!
0

You could use the eval() function. It runs any code given as argument. For example, to ++ each variable when its name appears in the array, use:

for(variablename in wA){ // or any other way to loop through the array
    eval(variablename + "++");
}

Also, you could use the owner object like an associative array. If the variables are global, just use window.

for(variablename in wA){ // or any other way to loop through the array
    window[variablename]++;
}

Comments

0

If your s variables are global then you could dot he following :

for (var i = wA.length; i--;)
  window[wA[i]] ++;

Comments

0

If they are global variables, you can grab them from the window object using bracket notation:

window[wA[0]]; // 0

Otherwsie, I would recommend using an object instead of variables:

var myVars = {
    s11 : 0,
    s12 : 0
};

myVars[wA[0]]; // 0

The only other way is using eval, which might be dangerous if you're dealing with user input.

Comments

0

If your variables are global you can do as shown below

s11 = 10;
s12 = 12;


var i = 11;

var wA = new Array(window["s" + i++], window["s" + i]);

Otherwise you will have to declare the variables in a scope , for e.g: using this

this.s11 = 10;
this.s12 = 12;


var i = 11;

var wA = new Array(this["s" + i++], this["s" + i]);

Or you can use a loop to push elements

var wA = [];
for(var i=11; i<=12; i++){
    wA.push(this["s" + i]);
}

NOTE: starting and end values of i depends on your requirement and implementation

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.