Refactoring a large block of chained if-else statements

后端 未结 5 1646
再見小時候
再見小時候 2020-12-19 15:57

This seems like overkill and I would like to refactor this...any suggestions

    if($(this).text() == \"Grocery\"){
        $(\".type_changer\").attr(\"id\",         


        
5条回答
  •  时光说笑
    2020-12-19 16:40

    var textToVal =
    {
        "Grocery" : "gro",
        "Restaurant" : "res"
        // and so on ...
    };
    
    for (var text in textToVal) {
        if ($(this).text() == text)
            $(".type_changer").attr("id", textToVal[text]);
    }
    

提交回复
热议问题