How to define 'contains' in an IF conditions

JLeong
Mega Sage

HI Guys,

Quick question, what is the operator for 'contains'?

My condition is: if category contains 'Europe'

How do I translate that?

I tried - if(categoryLikeEurope), it didnt work.

Thank you!

1 ACCEPTED SOLUTION

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

you have to use the function indexOf.

Please check below an example:

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");

JavaScript String indexOf() Method

Check also the example in the thread below, it will help you to understand how to use the indexOf in an IF condition:

https://community.servicenow.com/community?id=community_question&sys_id=aadc8729db9cdbc01dcaf3231f96...

If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you

Cheers
Alberto

View solution in original post

7 REPLIES 7

Happy to know that, welcome 🙂

Stewe Lundin
Mega Guru

Join the array and the use includes()

var arr = ["one", "two", "three", "four", "five"];
arr = arr.join(',');

gs.info(arr.includes("two"));
//returns   true

gs.info(arr.includes("foo"));
//returns   false

 

 

Pooja Mallikarj
Kilo Sage

Hi,

You can  also use contains() method to check for condition.

For example,

var v=[" Europe","England"];

if(v.contains("Europe"));

Please mark correct/helpful if it helps for you

Regards,

Pooja