Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置带有多个条件的if语句,该语句使用有效条件';if语句中的s变量?_Javascript_If Statement - Fatal编程技术网

Javascript 如何设置带有多个条件的if语句,该语句使用有效条件';if语句中的s变量?

Javascript 如何设置带有多个条件的if语句,该语句使用有效条件';if语句中的s变量?,javascript,if-statement,Javascript,If Statement,好吧,这个标题听起来有点疯狂。我有一个对象,我从一堆输入(来自用户)构建它。我根据接收到的值设置它们,但有时它们根本没有设置,这使得它们为空。我真正想做的是,为魔兽世界制作一个物品生成器。项目可以有多个属性,这些属性在用户看来都是相同的。以下是我的例子: +3 Agility +5 Stamina +10 Dodge 理论上,这应该只获取对象的属性名和键值,然后以相同的方式输出它。但是,如何设置该if语句 以下是我当前的if语句疯狂的样子: if(property == "agility")

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

+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语句,这太糟糕了,因为属性太多了。它基本上是在重复自己,但仍然一直使用属性名。我的JSFIDLE看起来是这样的:所以你可以自己玩它。谢谢

编辑:对了,我想做的是这样的:

if(property == "agility" || property == "stamina" || ....) {
    text = "+" + text + " " + THE_ABOVE_VARIABLE_WHICH_IS_TRUE;
}
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/>';
    }
}
这也很有意思。我绝对不想那样

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);
   }
}
小提琴:

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

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

对象:

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);
    }
}
如果您需要首字母大写:

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);
   }
}
小提琴:

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

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

对象:

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,
               ...
             }
然后你可以像这样显示它:

if(property == "agility" || property == "stamina" || ....) {
    text = "+" + text + " " + THE_ABOVE_VARIABLE_WHICH_IS_TRUE;
}
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 text='';
for(var键入属性){
//使用hasOwnProperty从Object.prototype中筛选出键
如果(h.hasOwnProperty(k)){
text=text+'h[k]+'+k+'
; } }
聊天后,代码显示如下:

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();
var item={};
item.name=“Thunderfury”;
item.rarity=“传奇”;
item.itemLevel=80;
item.Equipment=“拾取时绑定”;
item.unique=“unique”;
item.itemType=“剑”;
项目速度=1.90;
item.slot=“单手”;
item.damage=“36-68”;
项目1.dps=27.59;
item.attributes={
敏捷度:100,
耐力:200,
道奇:300
};
第1项耐久性=130;
item.chanceOnHit=“用闪电攻击你的敌人,造成209点自然伤害,然后跳跃到附近的其他敌人。每次跳跃都会使受害者的自然抵抗力降低17。影响5个目标。你的主要目标也会被旋风吞噬,使其攻击速度降低20%,持续12秒。”;
第1项要求=60;
函数构建(){
box=$('');//在内存中生成
用于(var输入项){
if(项目hasOwnProperty(键)){
如果(键=='attributes'){
for(item.attributes中的变量k){
if(item.attributes.hasOwnProperty(k)){
box.append('+'+item.attributes[k]+'+'+k++');
}
}
}否则{
框。追加(“”+项[键]+“”);
}
}
}
$(“#框”)。替换为(框);
}
build();

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

properties = { stamina: 10,
               agility: 45,
               ...
             }
然后你可以像这样显示它:

if(property == "agility" || property == "stamina" || ....) {
    text = "+" + text + " " + THE_ABOVE_VARIABLE_WHICH_IS_TRUE;
}
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 text='';
for(var键入属性){
//使用hasOwnProperty从Object.prototype中筛选出键
如果(h.hasOwnProperty(k)){
text=text+'h[k]+'+k+'
; } }
聊天后,代码显示如下:

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();
var item={};
item.name=“Thunderfury”;
item.rarity=“传奇”;
item.itemLevel=80;
item.Equipment=“拾取时绑定”;
item.unique=“unique”;
item.itemType=“剑”;
项目速度=1.90;
item.slot=“单手”;
item.damage=“36-68”;
项目1.dps=27.59;
item.attributes={
敏捷度:100,
耐力:200,
道奇:300
};
第1项耐久性=130;
item.chanceOnHit=“用闪电攻击你的敌人,造成209点自然伤害,然后跳跃到附近的其他敌人。每次跳跃都会使受害者的自然抵抗力降低17。影响5个目标。你的主要目标也会被旋风吞噬,使其攻击速度降低20%,持续12秒。”;
第1项要求=60;
函数构建(){
box=$('');//在内存中生成
用于(var输入项){
if(项目hasOwnProperty(键)){
如果(键=='attributes'){
for(item.attributes中的变量k){
if(item.attributes.hasOwnProperty(k)){
box.append('+'+item.attributes[k]+'+'+k++');
}
}
}否则{
框。追加(“”+项[键]+“”);
}
}
}
$(“#框”)。替换为(框);
}
build();

是否存在不会导致将其添加到文本中的属性?如果是这样的话,为什么不仅仅是像
text=“+”+文本+”+属性这样的东西@Prescott我可以这么做,但是