如何设置具有多个条件的if语句,该语句使用有效条件's变量
How to setup if-statement with multiple conditions, which uses the valid condition's variable in the if-statement?
好吧,这个标题听起来有点疯狂。我有一个对象,它是我根据(来自用户的)一堆输入构建的。我根据收到的值设置它们,但有时根本没有设置,这使它们为空。我真正想做的是,为魔兽世界制作一个物品生成器。项目可以具有多个属性,这些属性对用户来说都是相同的。这是我的例子:
+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/
相关文章:
- 这个条件语句的逻辑有问题
- 如何检查if/else语句中的随机条件
- 使用条件语句,使 jquery fadeIn/fadeOut 仅在视口 >= 480px 时才有效
- 照片滑块的条件语句
- 使用解析为javascript源的.php,如何使用条件语句将javascript代码封装在php括号之间
- KnockoutJS:为虚拟元素使用嵌套的条件if语句
- 在if语句注释的条件内指定变量的值
- Javascript/C# - 满足语句条件时的确认对话框
- 如何将窗口[VariableName]设置为if语句条件
- if 语句条件由 javascript 中 if 语句内的内容触发
- if 语句条件逻辑
- 函数声明,就像语句条件一样
- 如何减少“如果语句”条件?[减少 if 语句中的条件]
- if语句条件检查!= "undefined"失败
- If语句条件满足,但内容未执行
- AngularJS在ng-repeat语句中的if语句条件
- 变量中用作if语句条件的字符串
- Javascript如果语句条件不满足
- if 语句 - 条件评估时间(Javascript)
- 数组中指定的 if 语句条件