MortenMoulder
MortenMoulder

Reputation: 6646

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

Okay, that title will sound a bit crazy. I have an object, which I build from a bunch of inputs (from the user). I set them according to their value received, but sometimes they are not set at all, which makes them null. What I really want to do, it make an item generator for WoW. The items can have multiple attributes, which all look the same to the user. Here is my example:

+3 Agility
+5 Stamina
+10 Dodge

In theory, that should just grab my object's property name and key value, then output it in the same fashion. However, how do I setup that if-statement?

Here is what my current if-statement MADNESS looks like:

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

You get that point right? In WoW there are A TON of attributes, so it would suck that I would have to create an if-statement for each, because there are simply too many. It's basically repeating itself, but still using the property name all the way. Here is what my JSFiddle looks like: http://jsfiddle.net/pm2328hx/ so you can play with it yourself. Thanks!

EDIT: Oh by the way, what I want to do is something like this:

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

Which is hacky as well. I definitely don't want that.

Upvotes: 0

Views: 64

Answers (2)

trex005
trex005

Reputation: 5115

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

If you need the first letter capitalized :

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

UPDATE per comment:

If you already have an array of all the attributes somewhere, use that instead

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

UPDATE per next comment:

If you already have an object with the attributes as keys, you can use Object.keys(), but be sure to also employ 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);
   }
}

Fiddle: http://jsfiddle.net/trex005/rk9j10bx/

UPDATE to answer intended question instead of asked question

How do I expand the following object into following string? Note: the attributes are dynamic.

Object:

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

String:

+ 100 Agility + 200 Stamina + 300 Dodge

Answer:

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);
    }
}

Upvotes: 2

Prescott
Prescott

Reputation: 7412

It's unclear how you're getting these values an storing them internally - but assuming you store them in a hash table:

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

Then you could display it something like this:

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/>';
    }
}

After chat, code came out as follows:

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/

Upvotes: 1

Related Questions