Being quite new at JavaScript, I’m assuming the answer to my question is extremely simple and I just don’t know what I’m doing, but here goes:
How do I write an if statement that checks the value of an array when the array name is determined by a variable.
For example, say I’ve got something like this:
$x = 2
arr1 = [1, 1]
arr2 = [2, 2]
and I want to determine which array I examine based on the value of x. The way I’m trying it is below, but I’m quite certain that isn’t right:
if (arr$x = 1)
alert ('2)
You already know arrays – put them to use. An array can contain anything, including other arrays. Just store those two arrays in a third (“two-dimensional”) array and use
$xas index.Note that the example comparison will be false anyway, as you’re comparing an array (i.e. multiple values) to an integer (i.e. a single value).