Comparison between two string fields data

kumar37
Kilo Contributor

Hi All,

I Have a requirement to compare the two string fields data and the difference need to be populated in the 3rd field.

1st field name:  user input 

2nd field name: existing data

3rd field name: missing data:

1 st field data :  1,2,3,4,5,6

2nd field data : 2,5,6

 i need to compare the data between 1st field and 2nd field and difference should be populate in 3rd field.

3rd field data: 1,3,4

 

Thanks in advance,

kumar

 

1 ACCEPTED SOLUTION

I created an item in my dev instance and re-did the code.  Try this you will just need to update the variable names as I created them as a, b, and c.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	//Type appropriate comment here, and begin script below
	var a = g_form.getValue('a').toString().split(',');
	var b = g_form.getValue('b').toString();
	getDifference(a, b);
}
function getDifference(a, b)
{
//	var j = 0;
	var result = [];
	for (var i = 0; i < a.length; i++){
		if (b.toString().indexOf(a[i]) < 0){
			result.push(a[i]);
		}
	}
	
	g_form.setValue('c', result);
}

View solution in original post

30 REPLIES 30

can you post the code here. And also the field vaues

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var arr1=g_form.getValue("u_impacted_ci").split(',');
var arr2=g_form.getValue("u_cmdb_ci_data").split(',');
g_form.setValue("u_missing_servers",arr_diff(arr1,arr2));
function arr_diff (a1, a2) {

var a = [], diff = [];

for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}

for (var j = 0; j < a2.length; j++) {
if (a[a2[j]]) {
delete a[a2[j]];
} else {
a[a2[j]] = true;
}
}

for (var k in a) {
diff.push(k);
}

return diff.join();
}
}

 

note: i have replaced a2 field for loop value to "j".

u_impacted_ci data: adcadm03,adcadnis01,adchwe-mco10,adchwe-mco7,adchwe-mco8,adchwe-mco9,adcshaketable02,adcvadnis02,adcvcups,kumar,dileep,peddi

u_cmdb_ci_data: adcadm03,adcadnis01,adchwe-mco10,adchwe-mco7,adchwe-mco8,adchwe-mco9,adcshaketable02,adcvadnis02,adcvcups

 

ouput :

 

adcadnis01,adchwe-mco10,adchwe-mco7,adchwe-mco8,adchwe-mco9,adcshaketable02,adcvadnis02,adcvcups,kumar,dileep,peddi,
adcadnis01,
adchwe-mco10,
adchwe-mco7,
adchwe-mco8,
adchwe-mco9,
adcshaketable02,
adcvadnis02,
adcvcups,
,each,eachSlice,all,any,collect,detect,findAll,select,grep,include,member,inGroupsOf,inject,invoke,max,min,partition,pluck,reject,sortBy,toArray,zip,size,inspect,_reverse,_each,clear,first,last,compact,flatten,without,uniq,intersect,clone

 

thanks,

kumar

 

Do not change the function arr_diff() change it to 'i' from 'j', it worked for me with the function I posted. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}


var arr1=g_form.getValue("u_impacted_ci").split(',');
var arr2=g_form.getValue("u_cmdb_ci_data").split(',');
g_form.setValue("u_missing_servers",arr_diff(arr1,arr2));
function arr_diff (a1, a2) {

var a = [], diff = [];

for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}

for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}

for (var k in a) {
diff.push(k);
}

return diff.join();
}

}

Hi Abinay,

 

i have just used user code. even though it is giving me the same results:(

 

thanks,

kumar 

Strange! I just used the same code and it worked for me. Can you post the code here again