0

I'm trying to do this:

variable[playernumber][arrayposition] = thisvalue;

Should I instead be thinking of "variable" as an array object which itself contains an array? Perhaps something like:

variable[playernumber].subvariable[arrayposition]

Note: Number of players is theoretically infinite, so I can't use a switch.

3
  • What do you want your data to look like? What would your object with several players look like? Commented Mar 18, 2014 at 19:01
  • The data will be a single value, but I'd like to eliminate the if/thens in calling player0[i], player1[i], player2[i] by using player[0][i], player[1][i], player[2][i] Commented Apr 2, 2014 at 2:17
  • or more accurately, but calling player[p][i] where p stands for the current player. Commented Apr 2, 2014 at 2:34

2 Answers 2

1

What you're trying to do works. Consider the code below:

var array = [[]]; //declare array in array

array[0][0] = 'hello';

alert(array[0][0]); //<-- prints hello
Sign up to request clarification or add additional context in comments.

4 Comments

It was the declaration I wasn't getting. You rock, user1257538. You ROCK!
this seems to be choking when I try to initialize the second base set. Can't seem to get this to work: var test = [[]]; test[0][0]=0; test[1][0]=1; alert(test[0][0]+" "+test[1][0]);
The problem above is that test[1] is undefined. You can change your original declaration to var test = [[],[]]; Or you can add the line test[1] = [];
done and done. The [[],[]] works brilliantly. (My bad for not stating at the outset how much of a nub I am.) Thanks again for the great info!
1

Either approach is reasonable. However, if the first approach you suggested, which would be a two-dimension array, is sufficient for you needs, then that is the simpler approach. You could declare it like so:

var variable = [];

Then add arrays of values to that array like so, assuming playernumber is a variable with an integer value:

var someValues = [8, 17, -6, 34];
variable[playernumber] = someValues;

Then this line would show the number -6 in an alert box:

alert(variable[playernumber][2]);

By the way variable is a terrible name for a variable. Please don't actually use that.

4 Comments

As you mention, the initial approach is sufficient for the task at hand, but many thanks for elucidating the other method as I'll definitely have use for that down the road. You also rock, dgvid!
PS what is the preferred term for "variable". Would "array object" be optimal. (It actually took me a while to figure out how to even ask the question;)
Name variables for what they represent or the data they contain, not the data types. For example, if you have an array of arrays of player scores, name it playerScores. Document the data type, e.g., that it's an array of arrays of numbers, using a comment.
Thanks again. PS— You've shown me how how my most efficient method is actually to store all the values in a a single array, and call the specific value from the array position using a simple equation. (Since my intent is for others to be able to easily mod the program, I may still use an array of arrays of numbers for easy grokking.) Point being, I now have three methods of achieving the result. I really appreciate your taking the time to instruct me.

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.