ArrayUtil - Global

ArrayUtil API is a script include with useful functions for working with JavaScript arrays.

These methods are available to any server-side script.

ArrayUtil - concat(Array parent, Array child)

Merge two arrays.

Table 1. Parameters
Name Type Description
parent Array An array to merge
child Array An array to merge
Table 2. Returns
Type Description
Array An array of elements from both input arrays. Duplicates are not removed.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
 
gs.print("concat a1, a2: " + arrayUtil.concat(a1, a2));

Output: concat a1, a2: a,b,c,c,d,e

ArrayUtil - contains(Array array, Object element)

Searches the array for the element. Returns true if the element exists in the array, otherwise returns false.

Table 3. Parameters
Name Type Description
array Array The array to search
element Object The element to search for
Table 4. Returns
Type Description
Boolean True if the element is in the array, false otherwise.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
 
gs.print("Contains b: " + arrayUtil.contains(a1, "b"));
gs.print("Contains x: " + arrayUtil.contains(a1, "x"));
Output:
Contains b: true
Contains x: false

ArrayUtil - convertArray(Object a)

Convert an object to an array.

Table 5. Parameters
Name Type Description
a Object The object to be converted.
Table 6. Returns
Type Description
Array An array created from the object.


      
    

ArrayUtil - diff(Array a, Array b)

Finds the differences between two or more arrays.

Any number of arrays can be provided as parameters.

Table 7. Parameters
Name Type Description
a Array An array
b Array An array
Table 8. Returns
Type Description
Array Returns an array of items from array a that were not found in either array b or c, or other input arrays. Duplicates are removed from the result.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.diff(a1, a2));

Output: a,b

ArrayUtil - ensureArray(Object object)

Returns an array from the object.

Table 9. Parameters
Name Type Description
object Object The object from which to create an array.
Table 10. Returns
Type Description
Array An array created from the object.


      
    

ArrayUtil - indexOf(Array array, Object element)

Searches the array for the element. Returns the element index if found, -1 otherwise.

Table 11. Parameters
Name Type Description
array Array The array to search
element Object The element to search for
Table 12. Returns
Type Description
Number The index where the element was found, -1 otherwise.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
 
gs.print("indexOf b: " + arrayUtil.indexOf(a1, "b"));
gs.print("indexOf x: " + arrayUtil.indexOf(a1, "x"));
Output:
 indexOf b: 1
indexOf x: -1

ArrayUtil - indexOf(Array array, Object element, Number startIndex)

Searches the array for the element starting with the startIndex element. Returns the element index.

Table 13. Parameters
Name Type Description
array Array The array to search
element Object The element to search for
startIndex Number The index to begin the search
Table 14. Returns
Type Description
Number The position of the element in the array, or -1 if the element is not in the array.


      
    

ArrayUtil - intersect(Array a, Array b)

Finds the elements present in all arrays.

Any number of arrays can be provided as parameters.

Table 15. Parameters
Name Type Description
a Array An array
b Array An array
Table 16. Returns
Type Description
Array An array of elements from array a that were found in all of the other input arrays. Duplicates are removed.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.intersect(a1, a2));

Output: c

ArrayUtil - union(Array a, Array b)

Merge two or more arrays.

Any number of arrays can be provided as parameters.

Table 17. Parameters
Name Type Description
a Array An array
b Array An array
Table 18. Returns
Type Description
Array An array of items from all the input arrays. Duplicates are removed.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.union(a1, a2));

Output: a,b,c,d,e

ArrayUtil - unique(Array a)

Removes duplicate items from an array.

Table 19. Parameters
Name Type Description
a Array The array to check for duplicate elements.
Table 20. Returns
Type Description
Array An array of unique items from the input array.

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c", "c", "b");
gs.print(arrayUtil.unique(a1));

Output: a,c,b