use string “includes()” in switch Javascript case

前端 未结 4 657
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 06:45

In Javascript, is there a way to achieve something similar to this ?

const databaseObjectID = \"someId\"; // like \"product/217637\"

switch(databaseObjectID         


        
相关标签:
4条回答
  • 2021-01-02 07:11

    You usage would be considered an abuse of case.

    Instead just use ifs

         if (databaseObjectId.includes('product')) actionOnProduct(databaseObjectID); 
    else if (databaseObjectId.includes('user'))    actionOnUser(databaseObjectID); 
    // .. a long list of different object types
    

    If the ObjectId contains static content around the product or user, you can remove it and use the user or product as a key:

    var actions = {
      "product":actionOnProduct,
      "user"   :actionOnUser
    }
    
    actions[databaseObjectId.replace(/..../,"")](databaseObjectId);
    
    0 讨论(0)
  • 2021-01-02 07:21

    Question:

    use string “includes()” in switch Javascript case

    While the includes() method will work, it is case sensitive, and just matches any characters. I have found a Regex solution that I like much better, and provides a lot of flexibility. For example, you could easily change this to match only WORDS.

    var sourceStr = 'Some Text and literaltextforcase2 and more text'
    
    switch (true)  {  // sourceStr
    
      case (/LiteralTextForCase1/i.test(sourceStr)):
          console.log('Case 1');
          break;
    
      case (/LiteralTextForCase2/i.test(sourceStr)):
        console.log('Case 2');
        break;
    
      default:
          console.log('ERROR No Case provided for: ' + sourceStr);
    };
    
    //-->Case 2

    0 讨论(0)
  • 2021-01-02 07:24

    This will work, but it shouldn't be used in practice.

    const databaseObjectID = "someId"; // like "product/217637"
    
    switch(true) {
        case databaseObjectID.includes('product'): actionOnProduct(databaseObjectID); break;
        case databaseObjectID.includes('user'): actionOnUser(databaseObjectID); break;
        // .. a long list of different object types
    }
    
    0 讨论(0)
  • 2021-01-02 07:26

    Sorry, I'm a noob so someone will probably have to clean this up, but here is the idea. Pass to a function to check and return a category then use the switch.

    function classify(string){
      var category = categorize(string);
      switch (category) {
        case 'product':
          console.log('this is a product');
          break;
        case 'user':
          console.log('this is a user');
          break;
        default:
          console.log('category undefined');    
      }
    }
    
    function categorize(string){
      if (string.includes('product')){
        return 'product';
      }
      if (string.includes('user')){
        return 'user';
      }
    }
    
    classify("product789");
    classify("user123");
    classify("test567");
    

    Sorry, as well, for not matching your example.

    0 讨论(0)
提交回复
热议问题