简体   繁体   English

从jQuery中的数组动态创建参数

[英]Dynamically create parameter from array in jQuery

I'm using this plugin on my website, which works great. 我在我的网站上使用了这个插件 ,效果很好。 You can pass a parameter for state specific styles, it looks like this: 您可以为状态特定的样式传递参数,如下所示:

$('#map').usmap({

    showLabels: false,
    stateStyles: {
        fill : '#ffffff',
        stroke : '#4f4f4f',
        'stroke-width' : 4,
    },

    stateHoverStyles: {
        fill : '#ffffff'
    },

    stateSpecificStyles: {
        'IN': {
            fill : '#84b8e7'
        },

        'CA': {
            fill : '#84b8e7'    
        },

        'TX': {
            fill : '#84b8e7'    
        }
    },

    stateSpecificHoverStyles: {

        'IN': {
            fill : '#0972ce'    
        },

        'CA': {
            fill : '#0972ce'    
        },

        TX': {
            fill : '#0972ce'    
        }
    }
});

I would like to fill in those state styles dynamically from an array that looks like ["CA", "IN", "TX"]. 我想从看起来像[“ CA”,“ IN”,“ TX”]的数组中动态填充这些状态样式。 Is this possible? 这可能吗?

If you're just looking for a way to create an object that looks like that, this snippet will do it for you. 如果您只是在寻找一种创建看起来像这样的对象的方法,那么此代码段将为您完成。 Let us know more about what you have/need, and I can expand this. 让我们进一步了解您的需求/需要,我可以进行扩展。

 var arr = ['CA', 'IN', 'TX'], // Your state vars output = []; arr.forEach(function(el) { output[el] = { fill: '#000000' // Could come from the same array, if needed }; )}; console.log(output); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM