1

I am trying to setup an array of states and if a state or value exists in the array do a function. Below is the actual code i am trying to modify and the "stateRedirects" var is what i am trying to us in the if (code === stateRedirects ) { }...

var stateRedirects = [
    'US-TX',
    'US-CA',
    'US-AL'
    // more will be added
]

$('.map').vectorMap({
    map: 'us_en',
    backgroundColor: 'transparent',
    color: '#0071A4',
    hoverColor: false,
    hoverOpacity: 0.5,
    normalizeFunction: 'polynominal',
    scaleColors: ['#C8EEFF', '#0071A4'],
    onLabelShow: function(event, label, code){
        if (code === stateRedirects ) {
            //do nothing
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            event.preventDefault();
        }                   
    },      
    onRegionOver: function(event, code){
        if (code === stateRedirects ) {
            //do nothing
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            event.preventDefault();
        }                   
    },
    onRegionClick: function (event, code) {
        window.location = '/' + code;
    }   
});  

Per comments i changed it to use [ and ] but can not figure out how to now make it see if the code matches the array in this line if (code === stateRedirects ) {

Aaron
  • 525
  • 2
  • 9
  • 22
  • 2
    Is it `stateRedirects` an array? Then please use `[]` instead of `()`, which would result in evaluating to the last value. – Bergi Aug 01 '12 at 18:32
  • yes it is supposed to be an array but i do not know how to correctly get it to allow me to enter each state and use that variable down in the if statement to see if there is a state name that matches from the array. – Aaron Aug 01 '12 at 18:46

1 Answers1

1

Define your array of state code properly with square brackets. Then use array.contains functionality, which seems to be what you want.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • i modified the array to use [ and ] but am lost at modifying the if (code === stateRedirects ) to check if the code is equal to any of the items in the array. Thanks for the help! – Aaron Aug 01 '12 at 19:23
  • I'd linked to a bunch of solutions for that task. Where exactly are you stuck? – Bergi Aug 01 '12 at 19:33
  • Sorry but i am new to jquery and having a hard time making it see if the code === the array items. I tried making it code === $.inArray(code, stateRedirects) but that did not work. I am trying this if (code === 'US-TX' && 'US-CA' ) but cant get it to work either – Aaron Aug 01 '12 at 19:54
  • @Aaron - the `$inArray()` function returns the index in the array, or -1 if not found. Your test criteria for your if statement would be: `if ( $.inArray(code, stateRedirects) != -1 )` – Mads Hansen Oct 02 '12 at 01:14