如果匹配,则对数组中的每个项执行其他操作,如果使用jvectormaps

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

本文关键字:如果 执行 其他 jvectormaps 操作 数组      更新时间:2023-09-26

我正在尝试设置一个状态数组,如果数组中存在状态或值,则执行一个函数。下面是我试图修改的实际代码,"stateRedirects"var是我在if(code==stateRedirect){}。。。

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;
    }   
});  

根据注释,我将其更改为使用[和],但现在不知道如何使其查看代码是否与此行中的数组if (code === stateRedirects ) { 匹配

用方括号正确定义状态代码数组。然后使用array.contations功能,这似乎是您想要的。