Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

What you're trying to achieve is known as 'Variable variable'. Many programming languages supports it, but in Javascript there is no straightforward answer, so you have to use an array or object to contain the new variables.

Read about 'variable variable' for more details: Is there a JavaScript equivalent of PHP’s "variable variables"? Is there a JavaScript equivalent of PHP’s "variable variables"?

var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

What you're trying to achieve is known as 'Variable variable'. Many programming languages supports it, but in Javascript there is no straightforward answer, so you have to use an array or object to contain the new variables.

Read about 'variable variable' for more details: Is there a JavaScript equivalent of PHP’s "variable variables"?

var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

What you're trying to achieve is known as 'Variable variable'. Many programming languages supports it, but in Javascript there is no straightforward answer, so you have to use an array or object to contain the new variables.

Read about 'variable variable' for more details: Is there a JavaScript equivalent of PHP’s "variable variables"?

added 356 characters in body
Source Link
evilReiko
  • 20.6k
  • 24
  • 93
  • 111
var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

What you're trying to achieve is known as 'Variable variable'. Many programming languages supports it, but in Javascript there is no straightforward answer, so you have to use an array or object to contain the new variables.

Read about 'variable variable' for more details: Is there a JavaScript equivalent of PHP’s "variable variables"?

var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

What you're trying to achieve is known as 'Variable variable'. Many programming languages supports it, but in Javascript there is no straightforward answer, so you have to use an array or object to contain the new variables.

Read about 'variable variable' for more details: Is there a JavaScript equivalent of PHP’s "variable variables"?

Source Link
evilReiko
  • 20.6k
  • 24
  • 93
  • 111
var count = 5;
var answered = {};
for(var i=0;i<count;i++){
 var className = '.myclass'+i;
 answered['Q' + i] = $(className).map(function(){
 return $(this).text();
 }).get();
}

Now you can use answered.Q1 or answered['Q1'] to access first variable, Q2 for 2nd variable, etc.

lang-js

AltStyle によって変換されたページ (->オリジナル) /