Search code examples
javascriptjqueryarraysvariablesjvectormap

for each item in array if match do this else if using jvectormaps


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 ) {


Solution

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