0

From my DOM element, I let the user choose a state.

In my code I go:

 var State = $("select[name=state1]").val();

So if AZ was selected, the ouput would be State == "AZ"

Then I have arrays for every state so for example:

var AZ =[[1],[2]];

And I fill each array with some data. Then I try to do a calculation that looks like this:

var ABC = 25 * State[0][1];

However, the variable state isn't an array. I want state replaced by the chosen one, for example it might be AZ so I actually want to calculate...

 var ABC = 25 * AZ[0][1];

Any ideas on how I can pass the value of State into last equation automatically?

Badrush
  • 1,247
  • 1
  • 17
  • 35

2 Answers2

1

Change your state to be an object to contain state arrays:

var state = { AZ : [[1],[2]], OS : [[3], [4]] };
var abc = 25 * state["AZ"][0]
artm
  • 8,554
  • 3
  • 26
  • 43
  • 1
    What is OS? and what does the `:` do here? – Badrush Feb 26 '16 at 04:38
  • 1
    I'll take a guess and say it's an arbitrary state abbreviation data point used just as an example... And `:` assigns the array to the object's named data point. – J. Titus Feb 26 '16 at 04:49
  • 1
    @Badrush Just like jTitus said, 'OS' could be anything, just an example of another state, and : is how you assign values to object's properties. – artm Feb 26 '16 at 04:58
  • Okay, but if I am defining state as `var State = $("select[name=state1]").val();` I cannot make state a bunch of arrays. I basically get a string from the user, which I need to use to find the array whose name matches that string. – Badrush Feb 26 '16 at 14:01
  • @Badrush `var stateValues = { AZ : [[1],[2]], OS : [[3], [4]] }; var State = $("select[name=state1]").val(); var abc = 25 * stateValues[State][0]` – artm Feb 27 '16 at 02:06
  • `state = { AZ : [[1],[2]], OS : [[3], [4]] };` So here, it says `state` contains a matrix `AZ` with 2 rows, 4 columns.... and OS has 4 rows, 5 columns? – Badrush Feb 27 '16 at 02:28
  • Your solution has a wierd thing. When I do `var states = { AZ: [[1],[4]],` and I print AZ in console it has 2 rows but only 1 column. – Badrush Feb 27 '16 at 02:34
  • @Badrush Have a look at how arrays work in javascript. `var a = [[73, 83], [46]];` means an array of two arrays, not dimensions. `a[0]` will give you the first array `[73, 83]`, `a[1]` the 2nd array `[46]`. – artm Feb 27 '16 at 02:42
  • @artm how can I then call a specific element in the 2nd array then? `a[1][[0],[2]]` ? – Badrush Feb 27 '16 at 02:44
  • @Badrush `a[1]` gives you the 2nd array, to get the 2nd element inside the 2nd array you use `a[1][1]`. To get the 3rd element of the first array you use `a[0][2]`. To get the 4th element of the 2nd array you use `a[1][3]`. Like I said, have a look at how arrays in JS work. – artm Feb 27 '16 at 02:48
  • Okay but I don't want to deal with arrays that have only 1 dimension, I want an array with 2 rows. – Badrush Feb 27 '16 at 02:51
  • 1
    @Badrush http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – artm Feb 27 '16 at 03:00
1

I suggest assigning your state values a little differently:

var states = {
    AZ: [[1],[2]],
    //and so on
};

Then you can do

var ABC = 25 * states[State][0][1];
J. Titus
  • 9,535
  • 1
  • 32
  • 45
  • I dont understand what `AZ: [[1][2]]` does. State returns "AZ" from the DOM and then I want to call element `AZ[1][2]` by typing `State[1][2]` – Badrush Feb 26 '16 at 04:38
  • I forgot a comma. Edited the answer, but it's the same idea as @artm's. I'm just slower typing through the stack exchange app. lol – J. Titus Feb 26 '16 at 04:40
  • 1
    Basically the bottommost expression expands to 25 * the 2nd element of the array associated with attribute "AZ" of `states` (25 * 2) – J. Titus Feb 26 '16 at 04:44
  • Nice, I think that might work, I will test it at home tonight. – Badrush Feb 26 '16 at 14:01
  • Your solution has a wierd thing. When I do `var states = { AZ: [[1],[4]],` and I print AZ in console it has 2 rows but only 1 column. – Badrush Feb 27 '16 at 02:34
  • 1
    `AZ` is an array, that contains 2 arrays, with one element each. What were you expecting? – J. Titus Feb 27 '16 at 02:41
  • The nested arrays need to be 2 rows, 3 columns. – Badrush Feb 27 '16 at 02:42
  • 1
    So, `{AZ: [[1,2,3],[4,5,6]]}`? – J. Titus Feb 27 '16 at 02:46
  • if I wanted to change just one of those values later, how could I do it? Like if I wanted "5" to become "green" – Badrush Feb 27 '16 at 02:52
  • 1
    I think you should read about how objects and arrays work in JavaScript. Let's say you want to change the 4 to a 7. You would do `states.AZ[1][0] = 7;` or `states["AZ"][1][0] = 7;` – J. Titus Feb 27 '16 at 02:57
  • hey so I think your method works and I am able to call it using `states[State][1][4]` – Badrush Feb 27 '16 at 02:57