# How to check minimize or make if-condition on object loop more correct?

I am looping through an object and finding whether or not the object has the "key," along with some further stuff.

I feel that there is some other correct way to do this apart from this:

var initialLocalesGen = function(data){

var status;

if(!$.isEmptyObject(data)){ console.log(data); if(data.hasOwnProperty("username") && data.hasOwnProperty("name")){ var headerHtml = Handlebars.compile($("#header-template").html());
$("#userInfoBar").append(headerHtml({"username" : data["username"], "name" : data["name"]})); } if(data.hasOwnProperty("allLocales")){ var formHtml = Handlebars.compile($("#locale-template").html());
\$("form legend").after(formHtml(data["allLocales"])).end().find("span.submit").css({display:"block"});
}

if(data.hasOwnProperty("status") && parseInt(data["status"]) === 0){
currentStatus = 0;
var object1 = {"status" : data["status"] , "userLocales": data["userLocales"]}
userLocalesProcess(object1);
}

if(data.hasOwnProperty("status") && parseInt(data["status"]) === 1){
currentStatus = 1;
userChangeProcess({"changeLocales" : data["changeLocales"],"userLocales" : data["userLocales"] });
popupMaker({"message":data["message"],"status":data["status"]});
}

if(data.hasOwnProperty("status") && parseInt(data["status"]) === 2){
currentStatus = 2;
}

}

console.log("currentStatus:", currentStatus);

}


Could anyone help me minimize the condition or show me an even better way of doing it?

• Why do your objects always have different attributes? What is data? – mheinzerling Jun 28 '13 at 6:24
• data is object which i am getting from Backend, it contains all sub datas(values) – 3gwebtrain Jun 28 '13 at 6:26
• Of course data is an object and has values ;) More details please. – mheinzerling Jun 28 '13 at 6:28
• Found a nice link for checking properties in javascript – mheinzerling Jun 28 '13 at 6:41
• On a side-note, you should probably use either a switch or else-ifs for the conditions if they are exclusive, if more than one will never be true. It will make the relationships between the statements more clear. – tiffon Jul 28 '13 at 13:55

You could merge the last three ifs. (I assume there are no other states.)

if(data.hasOwnProperty("status")){
currentStatus =  parseInt(data["status"]);
switch(currentStatus ) {
case 0:
var object1 = {"status" : data["status"] , "userLocales": data["userLocales"]}
userLocalesProcess(object1);
break;
case 1:
userChangeProcess({"changeLocales" : data["changeLocales"],"userLocales" : data["userLocales"] });
popupMaker({"message":data["message"],"status":data["status"]});
break;
default:
break;
}

}


I think there is nothing else you can do here. All other ifs are totally different. Maybe you could extract method or variables do communicate the meaning of every branch, but beside that I thing we are done.

Of course, if is also suitable

...
if (currentStatus ==0) {
var object1 = {"status" : data["status"] , "userLocales": data["userLocales"]}
userLocalesProcess(object1);
} else if (currentStatus ==1){
userChangeProcess({"changeLocales" : data["changeLocales"],"userLocales" : data["userLocales"] });
popupMaker({"message":data["message"],"status":data["status"]});
}
...


Maybe you should just stay with obj.property == null. According to the link above it seems to be faster, but at least it is easier to read.

• But i red a article saying, we should try to avoid the switch cases.. – 3gwebtrain Jun 28 '13 at 6:24
• Why? Link? You can do the same with an if. – mheinzerling Jun 28 '13 at 6:25
• should we use the if or else if? – 3gwebtrain Jun 28 '13 at 6:27
• Update my post. Still interested in the article! – mheinzerling Jun 28 '13 at 6:32