Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a switch with multiple cases and getting the result as given in below code ,is there a shorter way or a better way for this

What I have tried:

var selectedCase=false;
             var selectedItem='';
            var item = ui.item;
              switch(item.type)
           {
               case 'brands':
               selectedCase=true;
               selectedItem= base_url+'brand/'+item.BrandUniqueName;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'brand/'+item.BrandUniqueName;

               break;
               case 'categories':
               selectedCase=true;
               selectedItem= base_url+'categories/'+item.CategoryUniquename;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'categories/'+item.CategoryUniquename;

               break;
               case "categoryBrand":
               selectedCase=true;
               selectedItem= base_url+'brand/'+item.BrandUniqueName+'?category='+item.CategoryUniquename;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'brand/'+item.BrandUniqueName+'?category='+item.CategoryUniquename;

               break;
               case 'offercategory':
               selectedCase=true;
               selectedItem=  base_url+'offercategory/'+item.OfferCategory;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'offercategory/'+item.OfferCategory;

               break;
               case 'vendorOfferCategory':
               selectedCase=true;
               selectedItem=base_url+'offers/'+item.UniqueName+'?category='+item.OfferCategory;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'offers/'+item.UniqueName+'?category='+item.OfferCategory;

               break;
               case 'vendor':
               selectedCase=true;
               selectedItem= base_url+'offers/'+item.VendorUniqueName;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'offers/'+item.VendorUniqueName;

               break;
               case 'offers':
               selectedCase=true;
               selectedItem= base_url+'offers/'+item.UniqueName;
               searchHeader.saveResult(selectedItem);
                window.location.href = base_url+'offers/'+item.UniqueName;

               break;
               case 'vendorcategory':
               selectedCase=true;
               selectedItem= base_url+'topcategories/'+item.UniqueName;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'topcategories/'+item.UniqueName;

               break;
               case 'vendors':
               selectedCase=true;
               selectedItem= base_url+'offers/'+item.UniqueName;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'offers/'+item.UniqueName;

               break;
               case 'products':
               selectedCase=true;
               selectedItem= base_url+'products/'+item.InternalLink;
               searchHeader.saveResult(selectedItem);
               window.location.href = base_url+'products/'+item.InternalLink;

               break;
               case "Not Available":
               //$("#autocomplete").val('');
               break;

           }
Posted
Updated 24-May-18 3:14am
Comments
itsmypassion 24-May-18 1:33am    
you can use arrays or object literals instead

https://stackoverflow.com/questions/2307157/alternative-to-the-switch-statement
https://toddmotto.com/deprecating-the-switch-statement-for-object-literals/

1 solution

You can reorganize your data so that you set a value prior to the switch and only modify it when necessary (and possible).

Eg:
selectedCase=true;

You can put that outside the switch and only modify it in the switch when
selectedCase=false;


Remove anything in your switch (loop, whatever) that either remains unchanged, as well. Tell the computer just once!

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900