-1

    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

请多多指教。

ps:仅供参考,我使用的是 EcmaScript 2015 / ES5。

4

3 回答 3

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');
}
于 2019-02-27T03:48:33.800 回答
0

解决此问题的一种可能解决方案是使用Array.some()(我认为它ES5基于此链接包含在内)。首先,您可以创建一个方法来检查 anelement是否在 an 上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');
}
于 2019-02-27T03:51:54.950 回答
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
    }
于 2019-02-27T03:46:09.190 回答