dimanche 5 avril 2015

How do I program many different conditions and actions

I want to loop through the level 1 properties in the object address and test a variety of conditions and actions that are specific to the property with an if statement within the loop. So, I've added level 2 condition and action properties in an attempt to reduce the complexity of the if statements. The code below represents what I'm trying to do and doesn't work. Not sure how to accomplish this.



var address = {
first: {
value: "Joe",
condition: "address[key].value.length === 0",
action: "console.log(key + ' Not street1:' + address[key].value.length)"
},
last: {
value: "Dirt",
condition: "address[key].value.length === 0",
action: "console.log(key + ' Not street1:' + address[key].value.length)"
},
street1: {
value: "123 Blah Street",
condition: "key === 'street1' && address[key].value.length === 0",
action: "console.log(key + ' This is street1')"
},
};

for (var key in address) {
var condition = address[key].condition;
var action = address[key].action;
if (condition) {
return action;
}
}

Aucun commentaire:

Enregistrer un commentaire