如何设置具有多个条件的if语句,该语句使用有效条件's变量

How to setup if-statement with multiple conditions, which uses the valid condition's variable in the if-statement?

本文关键字:语句 条件 变量 有效 设置 何设置 if      更新时间:2023-09-26

好吧,这个标题听起来有点疯狂。我有一个对象,它是我根据(来自用户的)一堆输入构建的。我根据收到的值设置它们,但有时根本没有设置,这使它们为空。我真正想做的是,为魔兽世界制作一个物品生成器。项目可以具有多个属性,这些属性对用户来说都是相同的。这是我的例子:

+3 Agility
+5 Stamina
+10 Dodge

理论上,应该只获取对象的属性名和键值,然后以相同的方式输出。但是,如何设置if语句?

以下是我当前的if语句"疯狂"的样子:

if(property == "agility") {
    text = "+" + text + " Agility";
}
if(property == "stamina") {
    text = "+" + text + " Stamina";
}
if(property == "dodge") {
    text = "+" + text + " Dodge";
}

你说得对吗?在魔兽世界中有很多属性,所以我必须为每个属性创建一个if语句,这太糟糕了,因为太多了。它基本上是在重复自己,但始终使用属性名称。以下是我的JSFiddle的样子:http://jsfiddle.net/pm2328hx/这样你就可以自己玩了。谢谢

编辑:哦,顺便说一下,我想做的是这样的事情:

if(property == "agility" || property == "stamina" || ....) {
    text = "+" + text + " " + THE_ABOVE_VARIABLE_WHICH_IS_TRUE;
}

这也很让人生气。我绝对不想那样。

if(['agility','stamina','dodge'].indexOf(property) !== -1){
   text = "+" + text + " " + property;
}

如果您需要第一个大写字母:

if(['agility','stamina','dodge'].indexOf(property) !== -1){
   text = "+" + text + " " + property.charAt(0).toUpperCase() + property.substr(1);
}

每条评论更新

如果你已经在某个地方有了一个所有属性的数组,那么就用它来代替

var myatts = [
   'agility',
   'stamina',
   'dodge'
];
if(myatts.indexOf(property) !== -1){
   text = "+" + text + " " + property.charAt(0).toUpperCase() + property.substr(1);
}

根据下一条评论更新

如果您已经有了一个以属性为键的对象,您可以使用object.keys(),但一定要使用hasOwnProperty

var item = {};
item.attribute = {
   agility:100,
   stamina:200,
   dodge:300
};
var property = "agility";
var text = "";
if(Object.keys(item.attribute).indexOf(property) !== -1){
   if(item.attribute.hasOwnProperty(property)){
      text = "+" + text + " " + property.charAt(0).toUpperCase() + property.substr(1);
   }
}

小提琴:http://jsfiddle.net/trex005/rk9j10bx/

更新以回答预期问题,而不是提问

如何将以下对象展开为以下字符串?注意:属性是动态的。

对象:

var item = {};
item.attribute = {
   agility:100,
   stamina:200,
   dodge:300
};

字符串:

+ 100 Agility + 200 Stamina + 300 Dodge

答案:

var text = "";
for(var property in item.attribute){
    if(item.attribute.hasOwnProperty(property)){
        if(text.length > 0) text += " ";
        text += "+ " + item.attribute[property] + " " + property.charAt(0).toUpperCase() + property.substr(1);
    }
}

目前尚不清楚您是如何获得这些值并将其存储在内部的,但假设您将它们存储在哈希表中:

properties = { stamina: 10,
               agility: 45,
               ...
             }

然后你可以把它显示成这样:

var text = '';
for (var key in properties) {
// use hasOwnProperty to filter out keys from the Object.prototype
    if (h.hasOwnProperty(k)) {
        text = text + ' ' h[k] + ' ' + k + '<br/>';
    }
}

聊天后,代码如下:

var item = {};
item.name = "Thunderfury";
item.rarity = "legendary";
item.itemLevel = 80;
item.equip = "Binds when picked up";
item.unique = "Unique";
item.itemType = "Sword";
item.speed = 1.90;
item.slot = "One-handed";
item.damage = "36 - 68";
item.dps = 27.59;
item.attributes = {
    agility:100,
    stamina:200,
    dodge:300
};
item.durability = 130;
item.chanceOnHit = "Blasts your enemy with lightning, dealing 209 Nature damage and then jumping to additional nearby enemies.  Each jump reduces that victim's Nature resistance by 17. Affects 5 targets. Your primary target is also consumed by a cyclone, slowing its attack speed by 20% for 12 sec.";
item.levelRequirement = 60;
function build() {
    box = $('<div id="box">'); //builds in memory
    for (var key in item) {
        if (item.hasOwnProperty(key)) {
            if (key === 'attributes') {
                for (var k in item.attributes) {
                    if (item.attributes.hasOwnProperty(k)) {
                        box.append('<span class="' + k + '">+' + item.attributes[k] + ' ' + k + '</span>');
                    }
                }
            } else {
                box.append('<span id="' + key + '" class="' + item[key] + '">' + item[key] + '</span>');
            }
        }
    }
    $("#box").replaceWith(box);
}
build();

http://jsfiddle.net/gp0qfwfr/5/