logo

NJP

Scripting: A Method For De-Duplicating an Object Array

Import · Jul 27, 2018 · article

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

DIFFICULTY LEVEL: INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.

You might be familiar with with the baseline ServiceNow Script Include utility library JSUtil which contains a really useful method named unique. This method is used to de-duplicate an array. Well, here is a great method for doing the same thing on an object array.

1. Copy and paste the following script into Scripts - Background (read the comments to understand what is happing in the code):

Script:

var location = 'S-B: UniqueObjectList Test';

var myObjectList = [];

try {
    // Build a list of objects with one duplicate
    var myObject = {};
    myObject.name = 'George Jetson';
    myObject.title = 'Building Architect';
    myObject.location = 'Dallas';
    // the key, is um, the key to the whole de-dup thing. It is the field used
    // to uniquely identify a particular record for de-duplication purposes
    myObject.key = (myObject.name + '|' + myObject.title + '|' + myObject.location).toLowerCase() + '';
    myObjectList.push(myObject);

    myObject = {};
    myObject.name = 'George Jetson'; // <-- the duplicate record
    myObject.title = 'Building Architect';
    myObject.location = 'Dallas';
    myObject.key = (myObject.name + '|' + myObject.title + '|' + myObject.location).toLowerCase() + '';
    myObjectList.push(myObject);

    myObject = {};
    myObject.name = 'Wilma Flintstone';
    myObject.title = 'Boss';
    myObject.location = 'Ft. Worth';
    myObject.key = (myObject.name + '|' + myObject.title + '|' + myObject.location).toLowerCase() + '';
    myObjectList.push(myObject);

    // print off the number of objects before the de-duplication
    gs.info('---> [{1}} myObjectList.length: {0}', [myObjectList.length, location]);

    myObjectList = uniqueObjectList(myObjectList);

    // print off the count of objects after the de-duplication
    gs.info('---> [{1}} myObjectList.length: {0}', [myObjectList.length, location]);

    // print off the keys of all the remaining objects.
    var message = 'List: \n';
    for (var i=0; i < myObjectList.length; i++) {
        var item = myObjectList[i];
        message += item.key + '\n'; 
    }
    gs.info('---> [{0}] {1}', [location, message]);
}
catch(err) {
    gs.error('---> [{1}} ERROR: {0}', [err, location]);
}

// note that this uses the JavaScript function splice to delete a member of an array
// loop through the list and compare it against itself. The key is used here for the
// match.
function uniqueObjectList(objectArray) {
    for( var i = 0; i < objectArray.length; i++){
        for( var j = i + 1; j < objectArray.length; j++){
            if( objectArray[j].key == objectArray[i].key ) {
                objectArray.splice(j, 1);  // delete the duplicate
                --j;  // reduce the array length by one
            }
        }
    }
    return objectArray;
}

2. Click on the Run script button. Your results will look like this.

*** Script: ---> [S-B: UniqueObjectList Test} myObjectList.length: 3.0*** Script: ---> [S-B: UniqueObjectList Test} myObjectList.length: 2.0*** Script: ---> [S-B: UniqueObjectList Test] List: george jetson|building architect|dallas

wilma flintstone|boss|ft. worth

Additional Notes:

  • The key field is what will be used to de-duplicate the list.
  • The key field MUST be unique or this method will not work.

Really any field can be used, but it would have to be unique, and you would have to adapt the function to accept the field.

Add it to your favorite Script Include utility library. I use this all the function quite a bit.

Enjoy!

Steven Bell.

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

image

Originally published on: 7-27-2018 06:49 AM

I updated the code and brought the article into alignment with my new formatting standard.

View original source

https://www.servicenow.com/community/developer-articles/community-code-snippet-scripting-a-method-for-de-duplicating-an/ta-p/2329915