mardi 26 février 2019

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.

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.

p.s. : Fyi, I'm using EcmaScript 2015 / ES5.

Aucun commentaire:

Enregistrer un commentaire