【问题标题】:Matching a String with Array of String in Java Script在Javascript中将字符串与字符串数组匹配
【发布时间】:2019-02-27 03:38:06
【问题描述】:

    str1 = booking_kode.substring(0, 3);
    B = ["800", "807", "826", "847", "866"];
    C = ["827", "846"];
    E = ["867", "879"];
    F = ["880", "899"];

    if (str1 = array B){
    	print ('Prefix , first 3 digit = ' + str1 + '\n')
    	comm_code = 'B000'
    	print ('Comm_Code = ' + comm_code + '\n')
    }
    else if (str1 = array C) {
	print ('Prefix , first 3 digit = ' + str1 + '\n')
	comm_code = 'C000'
	print ('Comm_Code = ' + comm_code + '\n')
}
    else if (str1 = array E) {
	print ('Prefix , first 3 digit = ' + str1 + '\n')
	comm_code = 'E000'
	print ('Comm_Code = ' + comm_code + '\n')
}
    else if (str1 = array F) {
	print ('Prefix , first 3 digit = ' + str1 + '\n')
	comm_code = 'F000'
	print ('Comm_Code = ' + comm_code + '\n')
}
    else {
	print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n')
	comm_code = 'D000'
	print ('Comm_Code = ' + comm_code + '\n')
}

你好,

我想知道如何将字符串 Str1 与数组 B,C,E,F 的值匹配。

我的意思是:

If Str1 = 800|| 807 || 826 || 847 || 866, Then Comm_code = B000
If Str1 = 827 || 846 then Comm_code = C000
If Str1 = 867 || 879 then Comm_code = E000
If Str1 = 880 || 899 then Comm_code = F000
Else Default --> Comm_code = D000

请多多指教。

附言: 仅供参考,我正在使用 EcmaScript 2015 / ES5。

【问题讨论】:

    标签: javascript arrays string if-statement ecmascript-5


    【解决方案1】:

    只需使用简单的String.prototype.indexOf

    str1 = booking_kode.substring(0, 3);
    B = ["800", "807", "826", "847", "866"];
    C = ["827", "846"];
    E = ["867", "879"];
    F = ["880", "899"];
    
    if (B.indexOf(str1) > -1)
    {
        print ('Prefix , first 3 digit = ' + str1 + '\n');
        comm_code = 'B000';
        print ('Comm_Code = ' + comm_code + '\n');
    }
    else if (C.indexOf(str1) > -1)
    {
        print ('Prefix , first 3 digit = ' + str1 + '\n');
        comm_code = 'C000';
        print ('Comm_Code = ' + comm_code + '\n');
    }
    else if (E.indexOf(str1) > -1)
    {
        print ('Prefix , first 3 digit = ' + str1 + '\n');
        comm_code = 'E000';
        print ('Comm_Code = ' + comm_code + '\n');
    }
    else if (F.indexOf(str1) > -1)
    {
        print ('Prefix , first 3 digit = ' + str1 + '\n');
        comm_code = 'F000';
        print ('Comm_Code = ' + comm_code + '\n');
    }
    else
    {
        print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
        comm_code = 'D000';
        print ('Comm_Code = ' + comm_code + '\n');
    }
    

    【讨论】:

    • 我已将完整代码添加到您的答案中,请随时编辑。
    【解决方案2】:

    您可以使用 Array.indexOf() 方法通过简单的 if else 条件来实现这一点。但是请确保 str1 和数组中的值具有相同的变量类型(字符串或数字)。

       if (B.indexOf(str1) > -1 ) {   //if value exists in B 
            //do soemthing;
        } 
        else if(C.indexof(str1) >-1 ) { //if value exists in C
          //do soemthing
        }
    

    【讨论】:

      【解决方案3】:

      解决此问题的一种可能解决方案是使用 Array.some()(我认为它包含在 ES5 中,基于此 link)。首先,您可以创建一个方法来检查 element 是否在 array 上:

      function arrayIncludes(arr, ele)
      {
           return arr.some(function(x) {return (x === ele);});
      }
      
      console.log(arrayIncludes([1,2], 2));
      console.log(arrayIncludes([1,2], 5));
      .as-console {background-color:black !important; color:lime;}

      然后,您的代码可以改写为:

      str1 = booking_kode.substring(0, 3);
      B = ["800", "807", "826", "847", "866"];
      C = ["827", "846"];
      E = ["867", "879"];
      F = ["880", "899"];
      
      function arrayIncludes(arr, ele)
      {
           return arr.some(function(x) {return (x === ele);});
      }
      
      if (arrayIncludes(B, str1))
      {
          print ('Prefix , first 3 digit = ' + str1 + '\n');
          comm_code = 'B000';
          print ('Comm_Code = ' + comm_code + '\n');
      }
      else if (arrayIncludes(C, str1))
      {
          print ('Prefix , first 3 digit = ' + str1 + '\n');
          comm_code = 'C000';
          print ('Comm_Code = ' + comm_code + '\n');
      }
      else if (arrayIncludes(E, str1))
      {
          print ('Prefix , first 3 digit = ' + str1 + '\n');
          comm_code = 'E000';
          print ('Comm_Code = ' + comm_code + '\n');
      }
      else if (arrayIncludes(F, str1))
      {
          print ('Prefix , first 3 digit = ' + str1 + '\n');
          comm_code = 'F000';
          print ('Comm_Code = ' + comm_code + '\n');
      }
      else
      {
          print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
          comm_code = 'D000';
          print ('Comm_Code = ' + comm_code + '\n');
      }
      

      【讨论】:

      • includes 是 ES6 - OP 只能使用 ES5。 indexOf 是正确答案。
      • 没问题@Shidersz。
      • @JackBashford 我已经修改了代码以使用Array.some()。我读过它可以从不同来源的ES5 获得。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2019-07-18
      • 2020-01-03
      • 1970-01-01
      • 2019-04-08
      • 2020-10-21
      相关资源
      最近更新 更多