0

I have seen posts like Declaring Multiple Variables in JavaScript that focus on unique variables (and lots of them), but I am looking at lots of non-unique variables, for example I want to do a much larger version of this...

            var rep;
            var state_def, state_MA, state_WY, state_TN = 'joeblow';
            var state_AZ, state_TX = 'moefoe';
            var state_MT, state_OR = 'growmo';
            var state_OK, state_LA = 'billyjoe';
            var state_WA, state_NM, state_MS = 'messytess';
            var state_VA, state_NY = 'slickrick';
            var state_CA = 'icecube';

            //or
            var state_def = state_MA = state_WY = state_TN = 'joeblow';

Just to understand, I am using a switch like so...

switch (capitalise(data.regionName)) {
                case 'Arizona': rep = state_AZ; break;
                case 'Texas': rep = state_TX; break;
                case 'Montana': rep = state_MT; break;
                case 'Oregon': rep = state_OR; break;
                case 'New Mexico': rep = state_NM; break;
                case 'Mississippi': rep = state_MS; break;
                case 'Oklahoma': rep = state_OK; break;
                case 'Louisiana': rep = state_LA; break;
                case 'Washington': rep = state_WA; break;
                case 'Massachusetts': rep = state_MA; break;
                case 'Wyoming': rep = state_WY; break;
                case 'Tennessee': rep = state_TN; break;
                case 'Virginia': rep = state_VA; break;
                case 'Illinois': rep = state_IL; break;
                case 'Nevada': rep = state_NV; break;
                default: rep = state_def; break;
            };

This will have several thousand zipcodes as well, so a secondary switch for this. That is why declaring the state (or zip) variable / user association as short as possible is needed. But I also do not want to fall into any pitfalls I might be unaware of. This works right now with the exception of the default: rep = state_def; break; returning as undefined. Also, does not work in IE8.

Community
  • 1
  • 1
Shane
  • 1,629
  • 3
  • 23
  • 50
  • When you do this var state_def, state_MA, state_WY, state_TN = 'joeblow'; state_def, state_MA and state_WY aren't assigned (== undefined). Only state_TN equal 'joeblow' – A. Wolff Nov 20 '12 at 18:32

1 Answers1

4

I suggest replacing your switch with a mapping:

var reps = {
    'Arizona': state_AZ,
    'Texas': state_TX,
    // etc.
};

// then you can do this:
var rep = reps[capitalize(data.regionName)];

Later, you could even download that mapping (including the values) as a JSON object or something from a server or other data source.

Notice also, that most of your state_* variables are undefined. I don't know if this is what you intended. If you only have data for some states, it might be better to build the data structure directly:

var reps = {
    'Arizona': undefined, // or just omit from mapping
    'Texas': 'moefoe',
    // etc.
};

and dispense with all the state_* variables.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • yes those are not all working and why I need a better solution, like yours ;-) I will write this out right now and run a test. – Shane Nov 20 '12 at 18:38
  • +1 All those `state_` variables could also be categorised into an object. – pimvdb Nov 20 '12 at 18:40
  • I suggest it was intended `var state_OK = state_LA = 'billyjoe';` , because the original version initializes the last variable only – Dan Nov 20 '12 at 18:43
  • Using a single object variable `state` instead of many `state_*` variables could be achieved in the following way: `var state = {}; state['OK'] = state['LA'] = 'billyjoe'; state['CA'] = 'icecube';` – Dan Nov 20 '12 at 18:48
  • The one thing I am trying to prevent is writing the user/url more than once. This would be the 'icecube', 'joeblow' etc. Because these will be the only things changed. – Shane Nov 20 '12 at 18:52
  • 1
    @Shane - If the user/url data need to change, then a two-level mapping might work. This would be especially true if, for instance, `state_AZ` and `state_TX` always were supposed to have the same user/url. So the mapping from `'Arizona'` and `'Texas'` would both map to `'AZ_and_TX'` (say), and you would then look in a second mapping object to find the value `'moefoe'`. – Ted Hopp Nov 20 '12 at 19:00
  • @Ted Hopp +1 for all the help. I have refined my script to your second response, by using **var reps = { 'Arizona': 'moefoe'**, Now I am thinking of a way to store this in a database so it can be updated by our IT department. For example have _location_ and _user_ as columns, then somehow recreate this mapping going from php to javascript. – Shane Nov 20 '12 at 20:28
  • @Shane - [JSON](http://www.json.org/) is ideal for this kind of thing. There's lots of JSON support for php. Not to mention that the "J" in JSON stands for "JavaScript", so there's plenty of support there. :) – Ted Hopp Nov 20 '12 at 21:34