简体   繁体   English

在 switch Javascript case 中使用字符串“includes()”

[英]use string “includes()” in switch Javascript case

In Javascript, is there a way to achieve something similar to this ?在 Javascript 中,有没有办法实现类似的东西?

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

switch(databaseObjectID) {
    case includes('product'): actionOnProduct(databaseObjectID); break;
    case includes('user'): actionOnUser(databaseObjectID); break;
    // .. a long list of different object types
}

This is more a curiosity question to understand the possibilities of switch / case, as in this particular case I have solved my problem using const type = databaseObjectID.split('/')[0];这更像是一个了解 switch / case 可能性的好奇问题,因为在这种特殊情况下,我已经使用const type = databaseObjectID.split('/')[0];解决了我的问题const type = databaseObjectID.split('/')[0]; and apply the switch case on type并在type上应用开关盒

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
}

You usage would be considered an abuse of case.您的使用将被视为滥用案例。

Instead just use ifs而只是使用 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:如果 ObjectId 包含围绕产品或用户的静态内容,您可以将其删除并使用用户或产品作为键:

var actions = {
  "product":actionOnProduct,
  "user"   :actionOnUser
}

actions[databaseObjectId.replace(/..../,"")](databaseObjectId);

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.也很抱歉,与您的示例不符。

Question:题:

use string “includes()” in switch Javascript case在 switch Javascript case 中使用字符串“includes()”

While the includes() method will work, it is case sensitive, and just matches any characters.虽然includes()方法可以工作,但它区分大小写,并且只匹配任何字符。 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.例如,您可以轻松地将其更改为仅匹配 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM