ArrayUtil Script include for playing around with arrays like a pro

ArrayUtil is an Out of the Box [OOB] script include which provides the ability to perform various cool functions related to Arrays. These functions will save you tons of custom lines of scripts and algorithms to play with arrays. It is documented HERE but I think some functions are not clear and need more details and that is what the goal of this article. 

ArrayUtil class requires an object to be created to access/call its functions. 

If you are writing a script in Global scope and need to leverage ArrayUtil then you can call it with its name.

var arr = new ArrayUtil();

If you need to call it from custom scope then you call it with its API NAME. 

var arr = new global.ArrayUtil();

This Utility contains 9 Methods/Functions which I will describe with example code and respective output. This Article Majorly talks about

– ensureArray method

– convertArray method

– diff method

– unique method

TRY EXAMPLE SCRIPTS IN BACKGROUND SCRIPT FOR A QUICK TEST 

 
1. ensureArray | want to convert a javascript object into an array?

This function takes an object as a single parameter. It converts the object and returns as a new Array. Please note it returns the whole object converted array at index 0.

Script

/* 

StudentInfoObj --> Object Name
arr --> Object of ArrayUtil Class to access its function.
studentInfoArray --> Array converted from Object.

Please note - index starts at 0 in an Array.

*/

var studentInfoObj = { 'name': 'Sharjeel', 'id': "RS030" }

var arr = new ArrayUtil();
var studentInfoArray = arr.ensureArray(studentInfoObj  /*Object Name*/);

gs.info("Name is " + studentInfoArray[0]['name']);
gs.info("ID is " + studentInfoArray[0]['id']);

Output

image

2. covertArray | want to convert a java object into an array?

Script

/* 

StudentInfoObj --> Object Name
arr --> Object of ArrayUtil Class to access its function.
studentInfoArray --> Array converted from Object.

Please note - index starts at 0 in an Array.

*/

// getMyGroups return loggedin user as JAVAOBJECT.
var myGroups = gs.getUser().getMyGroups();
gs.print(myGroups);

//print the type of myGroups object.
gs.print(Object.prototype.toString.call(myGroups));

// convert the JAVAOBJECT into the Array
var convertedArr = new ArrayUtil().convertArray(myGroups);
gs.print(convertedArr);

//print the type of convertedArray.
gs.print(Object.prototype.toString.call(convertedArr));

Output

image

To understand the difference between ensureArray & convertedArray see the example script below. 

Script

// Javascript object NOT JAVA OBJECT
var studentInfo = {

"Name" : "Muhammad",
"ID": "RS001"

};

var arrayUtil = new ArrayUtil();

var cArray = arrayUtil.convertArray(studentInfo); //Array returned by convertArray method
var eArray = arrayUtil.ensureArray(studentInfo); //Array returned by ensureArray method

gs.print('convertArray String --> ' + JSON.stringify(cArray)); // See the value of converted array in string format
gs.print('convertArray Type --> ' + Object.prototype.toString.call(cArray)); // See the type of converted array in string format

gs.print('ensureArray String --> ' + JSON.stringify(eArray)); // See the value of ensured array in string format
gs.print('ensureArray Type --> ' + Object.prototype.toString.call(eArray)); // See the type of ensured array in string format

Output

image 

3. diff | Want to remove duplicates between two or more arrays?

This function takes two or more arrays as parameters and returns the array of elements found in the first array but not in the rest of the arrays.

Script

/* 

studentNames1  --> 1st Array of Names.
studentNames2  --> 2nd Array of Student IDs. 
arr --> Object of ArrayUtil Class to access its function.
duplicatesRemoved --> Arrays of the element found in the first array but not in second.

Please note - Array passed in the second parameter will be appended/concatenated to first array.

*/

var studentNames1 = ['Muhammad', 'Sharjeel', 'Michael', 'Keshav'];
var studentNames2 = ['Sharjeel', 'Alex', 'Mubeen', 'Keshav'];

var arr = new ArrayUtil();
var duplicatesRemoved = arr.diff(studentNames1  /*1st Array*/, studentNames2 /*2nd Array*/);

gs.info("Student Concatenated Array --> " + duplicatesRemoved);

Output

image


4. unique | Want to remove duplicate elements in an array [single array]?

This function takes an array as a single parameter and returns the unique elements of the array [removed duplicates].

Script

/* 

studentNames  --> Array of Names, contains duplicate values.
arr --> Object of ArrayUtil Class to access its function.
uniqueElements --> Array of unique element, contains no duplicate.

*/

var studentNames = ['Muhammad', 'Sharjeel', 'Michael', 'Keshav', 'Sharjeel', 'Michael'];

var arr = new ArrayUtil();
var uniqueElements = arr.unique(studentNames  /* Array Name */);

gs.info("Unique Names are --> " + uniqueElements);

Output

image

REF – https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_ArrayUtilAPI

Leave a Comment

Your email address will not be published.