Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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
Tera Patron

Tai Vu
Kilo Patron
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