Why indexOf is returning always -1 even if element is present in array

Tarasingh26
Tera Expert

1000156084.jpg

Hi All,

 

I am implementing scenario of transform map script in which I need to check if category array contains element of source category and perform few tasks . But in attachment at 12 number line index is always return -1 even if element is present in categoryArr. Could someone please help this why it is returning always -1?

Thanks,

Tara Singh 

1000156084.jpg

1 ACCEPTED SOLUTION

maroon_byte
Mega Sage

 

As you mentioned your array is: Legal, Direct, Indirect

Hence your input value is "Direct" but your array contains " Direct"... leading space 🙂

 

But why convert to array? Try below:

var catStr = "Direct";
var categoryArr = "Legal, Direct, Indirect";
gs.info(categoryArr.toLowerCase().indexOf(catStr.toLowerCase()));
 
Note: Always compare strings with lower or upper case.

 

View solution in original post

12 REPLIES 12

Ramesh_Naidu
Mega Guru

Hi @Tarasingh26 ,

Did you try logging the variable categoryArr and checking its value?

Thankyou.

Values in categoryArr are coming correct. And source category field value is also there in categoryArr array but still it is returning -1.

@Tarasingh26 : Have you checked if there are any white spaces in the array values. If so, try removing those spaces.
Thankyou.

Amit Pandey
Kilo Sage

Hi @Tarasingh26 

 

Can you paste the code here in text format to check?

 

Regards,

Amit