简体   繁体   English

在Java脚本中将字符串与字符串数组匹配

[英]Matching a String with Array of String in Java Script

  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') } 

Hello, 你好,

I want to know how to match the string Str1 with the value of the Array B,C,E,F. 我想知道如何将字符串Str1与数组B,C,E,F的值匹配。

I mean : 我的意思是 :

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

Please kindly advice. 请指教。

ps : Fyi, I'm using EcmaScript 2015 / ES5. ps:Fyi,我正在使用EcmaScript 2015 / ES5。

Just use a simple String.prototype.indexOf : 只需使用一个简单的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');
}

You can achieve this with simple if else conditions using Array.indexOf() Methods. 您可以使用Array.indexOf()方法通过简单的else条件实现此目的。 However make sure the str1 and the values in the array are of same variable type(string or a number). 但是,请确保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
    }

One possible solution to solve this is use Array.some() (I think it is included on ES5 based on this link ). 解决此问题的一种可能方法是使用Array.some() (我认为基于此链接,它包含在ES5 )。 First you can create a method that will check if an element is on an array : 首先,您可以创建一个方法来检查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;} 

Then, your code can be reworked to this: 然后,可以将您的代码重新编写为:

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');
}

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

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