Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Comparison between two arrays

MounikaS
Tera Contributor

Hi Team,

Can some please help me how to compare 2 arrays and any difference found between we need to store difference values in array3 

 

 

     while (cm1.next()) {
			var array2 = req.split(', ');
        gs.info("array2 values" + array2);
        var array3 = [];
        var array4 = [];
            var listOfRequestors = cm1.list_of_requestors.getDisplayValue();
            var array1 = listOfRequestors.split(', ');
            gs.info("array1 values" + array1);

            for (var i = 0; i < array2.length; i++) {
                var trimmedValue = array2[i].trim();

                if (array1[i].indexOf(trimmedValue) !== -1) {
                    gs.info("Value in array2 found in array1: " + trimmedValue);
                    array4.push(trimmedValue);
                } else {
                    gs.info("Value in array2 not found in array1: " + trimmedValue);
                    gs.info("Array1: " + array1.join(', '));
                    gs.info("Array2: " + array2.join(', '));
                    array3.push(trimmedValue);
                }
            }
        }


        gs.info("Array3: " + array3.join(', '));
        gs.info("Array4: " + array4.join(', '));


        this.setAnswer(array3.join(', '));

 

 

Every time its going to else block even though if we have same values in both the arrays, can some please help me what i need to update to get only different values

2 REPLIES 2

Kristen Ankeny
Kilo Sage

I would use ArrayUtil diff for this: you can do diff for Array 1, Array 2; and then diff for Array 2, Array 1. Then use union to join those two results into your final array of where they do not overlap. https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/c_ArrayUtilAPI#r_AU-...

SanjivMeher
Mega Patron
Mega Patron

Can you please fix below line?

if (array1.indexOf(trimmedValue) != -1) {

 


Please mark this response as correct or helpful if it assisted you with your question.