Adding Var Object to An Array Dynamically
-
Hi all, I know that normally one can add string(s) to an array upon declaring the array by placing the string or strings inside the parenthesis, surrounded by double quotes and separating them with a comma. However, I would like to dynamically add them by doing the following but I cannot get it to work:
var stringArray[] = new Array(3);
//Declaring string objects
vary string1 = "First";
vary string2 = "Second";
vary string3 = "Third";//Then in a for loop add each string to the array
for(x=0; x
Please explain why the code above does not work, in detail thanks. -
Hi all, I know that normally one can add string(s) to an array upon declaring the array by placing the string or strings inside the parenthesis, surrounded by double quotes and separating them with a comma. However, I would like to dynamically add them by doing the following but I cannot get it to work:
var stringArray[] = new Array(3);
//Declaring string objects
vary string1 = "First";
vary string2 = "Second";
vary string3 = "Third";//Then in a for loop add each string to the array
for(x=0; x
Please explain why the code above does not work, in detail thanks.Your first line contains a syntax error, so none of the code will execute. You don't need the square brackets when you declare an array in Javascript (I think you do in Java, but it's been years since I did any).
var stringArray = new Array(3);
Apart from that, you have "vary" instead of "var" for your three strings, and you are terminating the loop one item too early. To use the actual string variables you declared, you have to
eval
the string you create using the loop index like this:= eval("string" + (x + 1));The "x + 1" is in parentheses so that the number x + 1 is appended to the string instead of x and 1 being appended separately.
-
Your first line contains a syntax error, so none of the code will execute. You don't need the square brackets when you declare an array in Javascript (I think you do in Java, but it's been years since I did any).
var stringArray = new Array(3);
Apart from that, you have "vary" instead of "var" for your three strings, and you are terminating the loop one item too early. To use the actual string variables you declared, you have to
eval
the string you create using the loop index like this:= eval("string" + (x + 1));The "x + 1" is in parentheses so that the number x + 1 is appended to the string instead of x and 1 being appended separately.
-
Hi all, I know that normally one can add string(s) to an array upon declaring the array by placing the string or strings inside the parenthesis, surrounded by double quotes and separating them with a comma. However, I would like to dynamically add them by doing the following but I cannot get it to work:
var stringArray[] = new Array(3);
//Declaring string objects
vary string1 = "First";
vary string2 = "Second";
vary string3 = "Third";//Then in a for loop add each string to the array
for(x=0; x
Please explain why the code above does not work, in detail thanks.Your code was not working because of the
[]
in declaration of your variablestringArray
Try this one out
var stringArray = new Array(3);
//Then in a for loop add each string to the array
for(x=0; x<stringArray.length-1; x ++)
{
//Add string
stringArray[x]= "string" + (x+1);
}If you add
"string"
+x+1 the output will be string11 string21 string31 I'm sure that's not what you want.