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

时间:2019-02-27 03:38:07

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

    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

请提出建议。

p.s。 :Fyi,我正在使用EcmaScript 2015 / ES5。

3 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:0)

您可以使用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
    }

答案 2 :(得分:0)

一种解决此问题的可能方法是使用 Array.some() (基于此link,我认为它已包含在ES5中)。首先,您可以创建一种方法来检查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');
}