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

@dhathrianoop 

please check script below and it gave me the difference as 1

var arrayUtil = new ArrayUtil();

var a = [1,2,3,4];

var b = [2,3,4,5];

gs.info(arrayUtil.diff(a, b));

Output:

find_real_file.png

find_real_file.png

Regards
Ankur

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

Namrata Khabale
Giga Guru

Hey dhathrianoop,

Check this thread, it might help you:

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

 

 Mark it Correct or Helpful, if it works based on impact....!!!!

 

Best Regards,

Namrata.