Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple variables inside for loop

I am trying to create multiple new variables inside a loop. The number of new variables depends on the lenght of another variable (variable "list" used below).

for(var i = 0; i < list.lenght; i++) 
{ 
var counter + i;   // create new variable (i.e. counter1, counter2,...)
}

I found a lot of very simmilar questions on StackOverflow, and the answer is mostly using an array (i.e. How do I create dynamic variable names inside a loop?).

If I use the suggested solution, do I create an array of variables? So in my case I will create multiple counters and I can then add values to that variables, i.e.:

counter6++;

If that is not the case how could I tackle the problem?

I apologize for asking you to explain an old answer, but I cannot comment in the old one because of low reputation.

like image 858
user2375263 Avatar asked Mar 11 '17 18:03

user2375263


People also ask

Can we use 2 variables in for-loop Python?

No. Using enumerate() we'll get an enumerate object.

Can you have two arguments in a for-loop?

With two arguments in the range function, the sequence starts at the first value and ends one before the second argument. Programmers use x or i as a stepper variable.

How do you create multiple variables in a single statement?

Syntax. var variable1 = value1, variable2 = value2, variable3 = value3; Here, var is the keyword used for declaring the variables viz: variable1, variable2, variable3 and assigning them the respective values.

Can we initialize two variables in for-loop in C?

Yes, C Compiler allows us to define Multiple Initializations and Increments in the “for” loop. The image below is the program for Multiple Initializations and Increments. In the above program, two variable i and j has been initialized and incremented. A comma has separated each initialization and incrementation.


2 Answers

You have some options here :

Create them global (not best practice ) :

for(var i = 0; i < list.lenght; i++){ 
  window['counter' + i] = 0;   // create counter1, counter2,...)
}

Use object :

var scope = {};
for(var i = 0; i < list.lenght; i++){ 
  scope['counter' + i] = 0;   // create scope.counter1, scope.counter2,...)
}

Use Object with with keyword

var scope = {};
for(var i = 0; i < list.lenght; i++){ 
  scope['counter' + i] = 0;   // create scope.counter1, scope.counter2,...)
}
with(scope){
 // here you can acesess keys in the scope object like them variable on the function scope
 counter0++
}

Use plain old Array

var scope = new Array(list.length);
like image 188
pery mimon Avatar answered Oct 01 '22 15:10

pery mimon


You can create an object, set property names to expected variable names, then use object destructuring assignment to get the property assignment or index of an object having a .length as a variable identifier; or use array destructuring to assign an identifier to a specfic index.

let [list, obj] = ["abc", {}];

for (let i = 0; i < list.length; i++) {
  obj["counter" + i] = list[i]
}

let {counter0, counter1, counter2} = obj;

console.log(counter0, counter1, counter2);

Alternatively

let list  = "abc";

let {0:counter0, 1:counter1, 2:counter2} = list;

console.log(counter0, counter1, counter2);

let list  = ["a","b","c"];

let [counter0, counter1, counter2] = list;

console.log(counter0, counter1, counter2);
like image 45
guest271314 Avatar answered Oct 01 '22 13:10

guest271314