Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Comparing two arrays : get the one which is not present in another array

dhathrianoop
Giga Expert

I have the below script :

Compare two arrays 'arrLaIpAddress' and 'arrCmdbIpAddress', if 'arrLaIpAddress' is not present in 'arrCmdbIpAddress', get those IP address in 'arrIpAddress'.

 

var ipAddress;
var arrLaIpAddress = [];
var arrCmdbIpAddress = [];
var arrIpAddress =[];
var grDiscoveryLogAnalysis = new GlideRecord('x_qune_da_log_analysis'); // UPDATE TABLE
grDiscoveryLogAnalysis.addQuery('troubleshooting.number', 'TRBL0001033');
grDiscoveryLogAnalysis.query();

while (grDiscoveryLogAnalysis.next()) {
//gs.log(grDiscoveryLogAnalysis.device_ip_address);

ipAddress = grDiscoveryLogAnalysis.getValue('device_ip_address');
arrLaIpAddress.push(ipAddress);
}
//gs.log(arrLaIpAddress);

var grCmdb = new GlideRecord('cmdb_ci');
grCmdb.addNotNullQuery('ip_address');
grCmdb.query();
while (grCmdb.next()) {
var cmdbIpaddress = grCmdb.ip_address;
arrCmdbIpAddress.push(cmdbIpaddress+'\n');

}

//gs.log(arrCmdbIpAddress);

for(var y = 0; y < arrLaIpAddress.length; y++){
if(arrCmdbIpAddress.indexOf(arrLaIpAddress[y]) < 0){
arrIpAddress.push(arrLaIpAddress[y]+'\n');
}
}
gs.log(arrIpAddress);

 

1 ACCEPTED SOLUTION

Ashley Snyder1
Giga Guru

Have you tried using the ServiceNow ArrayUtil method?

There's a built in 'diff' function that will compare two arrays. https://docs.servicenow.com/bundle/paris-application-development/page/app-store/dev_portal/API_refer...

View solution in original post

6 REPLIES 6

Ashley Snyder1
Giga Guru

Have you tried using the ServiceNow ArrayUtil method?

There's a built in 'diff' function that will compare two arrays. https://docs.servicenow.com/bundle/paris-application-development/page/app-store/dev_portal/API_refer...

I want only the element of 'arrLaIpAddress' if its not present in 'arrCmdbIpAddress'

Hi,

As already mentioned you need to use ArrayUtils diff method

https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_re...

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

a= [1,2,3,4]

b=[2,3,4,5]

by using ArrayUtils am getting [1,5]..but I want only [1]