简体   繁体   English

根据键从JS数组分配变量的值

[英]Assign value of variable from JS array based on key

I have a variable that takes its value from the current object's ID attribute, it will always look something like filtercolor-red : 我有一个变量,它从当前对象的ID属性获取其值,它将始终看起来像filtercolor-red

$('.facet-options-list li input[id*=filtercolor]').each(function() {
    var filterColor = $(this).attr('id');
    ...
});

I also have an array that lists possible ID's and a corresponding HEX code: 我也有一个列出可能的ID和对应的十六进制代码的数组:

var activeFilterBg;
var filterBgColor = [];
filterBgColor = {
    filtercolor-black:      '#171710',
    filtercolor-blue:       '#4C94B6',
    filtercolor-brown:      '#50443D',
    filtercolor-gold:       '#F6D069',
    filtercolor-green:      '#96B14D',
    filtercolor-grey:       '#A8AAA5',
    filtercolor-orange:     '#DB5E46',
    filtercolor-pink:       '#E78EB1',
    filtercolor-purple:     '#59547E',
    filtercolor-red:        '#D22200',
    filtercolor-silver:     '#EBEBEB',
    filtercolor-white:      '#FFF'
};

What I'd like to do is take filterColor and assign activeFilterBg the appropriate HEX code from filterBgColor . 我想做的是获取filterColor并从filterBgColor分配activeFilterBg适当的十六进制代码。 I could do this with a switch , but that seems kind of sloppy and gives a lot of room for mistakes in the future. 我可以通过switch来完成此switch ,但这似乎有点草率,并且在将来可能会出现很多错误。

Do I have an option to somehow lookup the correct key and then assign a variable based upon it? 我是否可以选择以某种方式查找正确的密钥,然后根据其分配变量?

That's not an array, that's an object. 那不是数组,那是一个对象。 You assign an array to the variable, but then you immediately replace that with an object. 您为变量分配了一个数组,但随后立即将其替换为对象。 An object works well for this, so just skip that array. 一个对象为此很好地工作,因此只需跳过该数组即可。 (Note though, as Jordan pointed out, that the parameter names has to be quoted when they contain dashes.) (不过,请注意,正如Jordan所指出的,当参数名称包含破折号时,必须将其引起引用。)

You can use the bracket syntax to access the object properties using the variable: 您可以使用方括号语法使用变量来访问对象属性:

var activeFilterBg;
var filterBgColor = {
    'filtercolor-black':      '#171710',
    'filtercolor-blue':       '#4C94B6',
    'filtercolor-brown':      '#50443D',
    'filtercolor-gold':       '#F6D069',
    'filtercolor-green':      '#96B14D',
    'filtercolor-grey':       '#A8AAA5',
    'filtercolor-orange':     '#DB5E46',
    'filtercolor-pink':       '#E78EB1',
    'filtercolor-purple':     '#59547E',
    'filtercolor-red':        '#D22200',
    'filtercolor-silver':     '#EBEBEB',
    'filtercolor-white':      '#FFF'
};

$('.facet-options-list li input[id*=filtercolor]').each(function() {
    var filterColor = $(this).attr('id');
    activeFilterBg = filterBgColor[filterColor];
});

Check the object first with hasOwnProperty - and if the property exists, use it! 首先使用hasOwnProperty检查对象-如果该属性存在,请使用它!

$('.facet-options-list li input[id*=filtercolor]').each(function() {
    var filterColor = this.id; //$(this).attr('id');
    var color;
    if (filterBgColor.hasOwnProperty(filterColor) {
        color = filterBgColor[filterColor];
    } else {
        color = "#FFF"; //not found
    }
});

mapping the id to the color should be as simple as 将id映射到颜色应该很简单

var filterColor = $(this).attr('id');
var hexCode = filterBgColor[filterColor];
//then do whatever with the hexCode

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

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