We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Remove matching entries from an array

Rakshanda Kunte
Tera Contributor

Hi All,

 

Requirement:

I have 2 arrays.

array1 = ['Software', 'Hardware', 'Service Catalog']

array2 = ['Technology', 'Governance', 'Hardware', 'HR', 'Service Desk', 'Service Catalog', 'Software']

 

If whatever present in array1, is matching the names in array 2, then remove those names from array 2 and give the new array.

 

Output should be:

array2 = ['Technology', 'Governance', 'HR', 'Service Desk']

 

Kindly, help with scripting.

6 REPLIES 6

Maik Skoddow
Tera Patron

Tai Vu
Kilo Patron

Hi @Rakshanda Kunte 

Let's try ServiceNow ArrayUtil.

var array1 = ['Software', 'Hardware', 'Service Catalog'];
var array2 = ['Technology', 'Governance', 'Hardware', 'HR', 'Service Desk', 'Service Catalog', 'Software'];

var result = new ArrayUtil().diff(array2, array1);
gs.log(result);

 

Ref: ArrayUtil - Global

 

Cheers,

Tai Vu