Refactoring a large block of chained if-else statements

后端 未结 5 1763
再見小時候
再見小時候 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:35

    Have an array that contains the value (eg: Salon, Grocery) and also contains the replacement (eg: sal, gro) and replace with that. The alternative is to simply take the first 3 letters and lowercase them but this may cause issues with things that contain only 2 letters then a space if we assume you'll be expanding the list, if not you can go with simply lowercasing the first 3 letters. If you need code examples, just say :)

提交回复
热议问题