1
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ Why do your objects always have different attributes? What is data? \$\endgroup\$ Commented Jun 28, 2013 at 6:24
  • \$\begingroup\$ data is object which i am getting from Backend, it contains all sub datas(values) \$\endgroup\$
    – 3gwebtrain
    Commented Jun 28, 2013 at 6:26
  • \$\begingroup\$ Of course data is an object and has values ;) More details please. \$\endgroup\$ Commented Jun 28, 2013 at 6:28
  • \$\begingroup\$ Found a nice link for checking properties in javascript \$\endgroup\$ Commented Jun 28, 2013 at 6:41
  • \$\begingroup\$ 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. \$\endgroup\$
    – tiffon
    Commented Jul 28, 2013 at 13:55

1 Answer 1

1
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.