简体   繁体   English

如何为我的JavaScript正确编写此数组

[英]How do I correctly write this array for my javascript

I have run into this problem again and now really need to know how to correctly write this array. 我又遇到了这个问题,现在真的需要知道如何正确地写这个数组。

I have a drop-box that, based on the value selected, will display a particular field-set(s) via the ID of the field-set. 我有一个基于所选值的下拉框,它将通过字段集的ID显示特定的字段集。

In some cases, two different values from the drop-box may call the same field-sets. 在某些情况下,下拉框中的两个不同值可能会调用相同的字段集。 So for example, if I choose banners , several field-sets will be displayed, if I choose Homepage Updates , the same field-sets would be displayed. 因此,例如,如果我选择banners ,将显示几个字段集,如果我选择“ 首页更新” ,则将显示相同的字段集。 So I want my array to basically be>> if the value selected in project type drop-box equals Banners or Homepage Updates , show the specified field-sets. 因此,如果项目类型下拉框中选择的值等于BannersHomepage Updates ,则显示指定的字段集,因此我希望数组基本上是>>。 The problem is, only the first option works. 问题是, 只有第一个选项有效。 In this case Banners. 在这种情况下,横幅。 When I select Homepage Updates, nothing is displayed. 当我选择“主页更新”时,什么也不显示。

Does anyone know who to write this array correctly? 有人知道谁可以正确地写这个数组吗? I have a ton of types that I have to code and the array is the clearest way to write it, but I can't get past this hurdle. 我必须编写大量的类型,数组是编写它的最清晰的方法,但是我无法克服这个障碍。 Help please. 请帮助。 A portion of the array is below. 数组的一部分在下面。

var projectTypes = new Array (
  {id : '660' , value:('Banners' ||'Homepage Updates')},
  {id : '659' , value:('Banners' || 'Homepage Updates')},
  {id : '661' , value:'Banners'}, 
  {id : '662' , value:'Banners'},   
  {id : '663' , value:'Banners'}, 
  {id : '668' , value:'Redirect'}, 
  {id : '229' , value:'Affiliates'},
  {id : '236' , value:'Affiliates'},
  {id : '242' , value:'Affiliates'},
  {id : '250' , value:'Affiliates'},
  {id : '251' , value:'Resources'},
  {id : '375' , value:'Resources'},
  {id : '376' , value:'Resources'},
  {id : '377' , value:'Ads'},
  {id : '237' , value:'Ads'}
);

Firstly, your expression ('Banners' ||'Homepage Updates') will always be evaluated to 'Banners' . 首先,您的表达式('Banners' ||'Homepage Updates')将始终被评估为'Banners'
The logical operator OR ( written as || ) is evaluated as : 逻辑运算符OR (写为|| )计算为:

boolean OR boolean

Since the first expression's argument is a String that is not empty, it is automatically converted to a true value (because of the automatic type-conversion. 由于第一个表达式的参数是一个不为空的字符串,因此它会自动转换为true值(由于自动类型转换)。

In your case, I suggest You use an array of possible values to for your objects. 对于您的情况,我建议您对对象使用一组可能的值。
This way, you can choose the appropriate one: 这样,您可以选择适当的一种:

var projectTypes = new Array (
  {id : '660' , value: ['Banners', 'Homepage Updates']},
  {id : '659' , value: ['Banners', 'Homepage Updates']},
  {id : '661' , values: ['Banners']}, 
  {id : '662' , values: ['Banners']},   
  {id : '663' , values: ['Banners']}, 
  {id : '668' , values: ['Redirect']}, 
  {id : '229' , values: ['Affiliates']},
  {id : '236' , values: ['Affiliates']},
  {id : '242' , values: ['Affiliates']},
  {id : '250' , values: ['Affiliates']},
  {id : '251' , values: ['Resources']},
  {id : '375' , values: ['Resources']},
  {id : '376' , values: ['Resources']},
  {id : '377' , values: ['Ads']},
  {id : '237' , values: ['Ads']}
);

If I understand correctly and you want to access some ids based on a value, you could organize your objects like this : 如果我理解正确,并且您想基于值访问某些ID,则可以像这样组织对象:

var projectTypes = {
    'Banners' : [660, 659, 661, 662, 663],
    'Homepage Updates' : [660, 659],
    'Redirect' : [668], 
    'Affiliates' : [229, 236, 242, 250],
    'Resources' : [251, 375, 376],
    'Ads' : [237, 377]
};

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

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