Help with comparing the differences between 2 arrays

sndev1099
Giga Expert

Hi, 

I need to compare 2 arrays. Find the sysids from array Array1 and see if they exist in Array2. If they don't add them to array3. The arrays are displaying correctly but when I try to log array3 to find the difference, it just seems to be logging the values in array1. Code below. Tried using the arrayUtil as well but was getting the same result

	var array1 = [];
	var array2 = [];
	var array3 = [];
	
	var legalEnt = new GlideRecord('u_department_mapping_rules');
	legalEnt.addQuery('u_department_name', current.u_department_name);
	legalEnt.query();
	
	while (legalEnt.next()) {
		array1.push(legalEnt.u_linked_legal_entity.toString());
	}
	gs.log("AB1601 array is " + array1);
	
	var compM2m = new GlideRecord('u_m2m_company_department');
	compM2m.addQuery('u_department', current.u_department_name);
	compM2m.query();
	while (compM2m.next()) {
		
		array2.push(compM2m.u_company.sys_id);
	}
	for(var i = 0; i < array1.length; i++){
		if(array2.indexOf(array1[i]) == -1) {
        array3.push(array1[i]);
		}
	}
		gs.log("AB160199 " + array3);

 

 

1 ACCEPTED SOLUTION

can you give this a go

	var array1 = [];
	var array2 = [];
	var array3 = [];
	
	var legalEnt = new GlideRecord('u_department_mapping_rules');
	legalEnt.addQuery('u_department_name', current.u_department_name);
	legalEnt.query();
	
	while (legalEnt.next()) {
		array1.push(legalEnt.u_linked_legal_entity.toString());
	}
	gs.log("AB1601 array is " + array1);
	
	var compM2m = new GlideRecord('u_m2m_company_department');
	compM2m.addQuery('u_department', current.u_department_name);
	compM2m.query();
	while (compM2m.next()) {
		
		array2.push(compM2m.u_company.sys_id +'');
	}
	for(var i = 0; i < array1.length; i++){
		if(array2.indexOf(array1[i]) == -1) {
        array3.push(array1[i] +'');
		}
	}
		gs.log("AB160199 " + array3);

View solution in original post

9 REPLIES 9

Ahmed Drar
Tera Guru
Tera Guru

have you tried to write your code like this

	var array1 = [];
	var array2 = [];
	var array3 = [];
	
	var legalEnt = new GlideRecord('u_department_mapping_rules');
	legalEnt.addQuery('u_department_name', current.u_department_name);
	legalEnt.query();
	
	while (legalEnt.next()) {
		array1.push(legalEnt.u_linked_legal_entity.toString());
	}
	gs.log("AB1601 array is " + array1);
	
	var compM2m = new GlideRecord('u_m2m_company_department');
	compM2m.addQuery('u_department', current.u_department_name);
	compM2m.query();
	while (compM2m.next()) {
		
		array2.push(compM2m.u_company.sys_id);
	}
	for(var i = 0; i < array1.length; i++){
		if(array2.indexOf(array1[i]) == -1) {
        array3.push(array1[i] +'');
		}
	}
		gs.log("AB160199 " + array3);

Please mark my answer as Correct / Helpful based on the Impact.

Hi Ahmed

Thanks for the response. Still just seems to log the sysids of array1

can you give this a go

	var array1 = [];
	var array2 = [];
	var array3 = [];
	
	var legalEnt = new GlideRecord('u_department_mapping_rules');
	legalEnt.addQuery('u_department_name', current.u_department_name);
	legalEnt.query();
	
	while (legalEnt.next()) {
		array1.push(legalEnt.u_linked_legal_entity.toString());
	}
	gs.log("AB1601 array is " + array1);
	
	var compM2m = new GlideRecord('u_m2m_company_department');
	compM2m.addQuery('u_department', current.u_department_name);
	compM2m.query();
	while (compM2m.next()) {
		
		array2.push(compM2m.u_company.sys_id +'');
	}
	for(var i = 0; i < array1.length; i++){
		if(array2.indexOf(array1[i]) == -1) {
        array3.push(array1[i] +'');
		}
	}
		gs.log("AB160199 " + array3);

Thank you Ahmed! What was I doing wrong?