Refactoring a large block of chained if-else statements

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

    Use class instead of id as inner wrapper

    use array to store your custom text for matching later

    $('.selector').each(function() {}

    in this way you can make process common

    use array replace method

    from = ['Grocery','Restaurant']
    to = ['gro','res']
    

提交回复
热议问题