Question:
Javascript Arrays - Constructed in code?
henrimaselati
2010-03-15 08:21:40 UTC
hi,
how can i refer to an array for example:

arr1=[1,2,3,4]
arr2=[7,6,5,4]
arr3=[6,23,45,24]

for (i=1;i<4;i++){
ARRR=arr+i // <----------- this is what i want to do, but it doesnt work, any ideas?
if (ARRR[0]==7) the_function_i_want_to_call();
}

plz dont tell me why i am doing it, this is just an example to be able to loop thru the arrays: arr
Five answers:
AnalProgrammer
2010-03-15 08:43:32 UTC
You would be better off creating a 2 dimensional array

int[][] arr = {{1,2,3,4},{7,6,5,4},{6,23,45,24}};



Then in the for loop

ARRR = arr[i];



Have fun.
Frank
2010-03-15 08:42:17 UTC
I think i see what you're trying to do and it's use arr+1 to equal the variable arr1? I don't know of a way to do that, but

you can use 2 dimensional arrays instead.



var arr = new Array(3);

arr[0] = [1,2,3,4];

arr[1]= [7,6,5,4];

arr[2]=[6,23,45,24];



for (i=0;i<3;i++){

ARRR=arr[i];

if (ARRR[0]==7)

the_function_i_want_to_call();

}



Additional Info:

I just checked and there is an eval() function in javascript, this also works



var arr1=[1,2,3,4];

var arr2=[7,6,5,4];

var arr3=[6,23,45,24];



for (i=1;i<4;i++){

eval("ARRR=arr"+i)

if (ARRR[0]==7) alert(ARRR[0]);

}
V2
2010-03-15 08:39:51 UTC
Are you trying to sum all elements of the arrays? If so this is just a completely wrong way to do it.



IF you want to sum all array elements in the variable ARRR and see if the sum is 7 than you should have



int ARRR = 0;

for (int i=0; i < arr1.length; i++)

ARRR+=arr1[i];



and do the check for arr2, arr3, etc.





It is just unclear what exactly you mean by "this is what i want to do, but it doesnt work, any ideas"
ferrell
2016-10-24 05:28:38 UTC
In JavaScript, Math.round() chooses a form from between 0 and a million. So it will be a decimal value. If all 3 pictures are decrease than a million (which they're going to be) they are going to pick an image with the aid of default and choose it. Multiply the random form with the aid of 10 and round. Then it will be a flat a million-10 answer. Math.round(Math.random()*10); reliable success! in case you want extra asistance/suggestion, be happy to the contact me.
Andres
2010-03-15 08:52:11 UTC
in php theres a way to do this using the eval code



i know you can do what you are referring to with strings but I am not too sure about javascript, i just checked w3schools and I didn't see anything similar to php's eval



I do the same type of dynamic variable creation



www.w3schools.com/js



Sorry to not be more precise


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...