---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# ArrayUtil - Global

# ArrayUtil - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 6 minutes to read

The ArrayUtil script include provides methods for working with JavaScript arrays.

These methods are available to any server-side script.

## ArrayUtil - concat(Array parent, Array child) {#ariaid-title2}

Merge two arrays.
{#r_AU-concat_A_A__table_ny5_yxz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| parent | Array | An array to merge |
| child | Array | An array to merge |
[Table 1. Parameters]

{#r_AU-concat_A_A__table_ny5_yxz_nt} {#r_AU-concat_A_A__table_oy5_yxz_nt__entry__2}

| Type | Description |
|-|-|
| Array | An array of elements from both input arrays. Duplicates are not removed. |
[Table 2. Returns]

{#r_AU-concat_A_A__table_oy5_yxz_nt}  

    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) {#ariaid-title3}

Searches the array for the specified element. Returns true if the element exists in the
array, otherwise returns false.
{#r_AU-contains_A_O__table_zbc_w5z_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| array | Array | Array to search. |
| element | Object | Element to search for. |
[Table 3. Parameters]

{#r_AU-contains_A_O__table_zbc_w5z_nt} {#r_AU-contains_A_O__table_acc_w5z_nt__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag indicating whether the element was found in the array. Possible values: * true: Element found in array. * false: Element not found in array. {#r_AU-contains_A_O__ul_k2b_r45_knb} |
[Table 4. Returns]

{#r_AU-contains_A_O__table_acc_w5z_nt}  

    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) {#ariaid-title4}

Converts a Java object to an array.
{#r_AU-convertArray_O__table_m5s_mb1_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| a | Object | Object to convert. |
[Table 5. Parameters]

{#r_AU-convertArray_O__table_m5s_mb1_4t} {#r_AU-convertArray_O__table_n5s_mb1_4t__entry__2}

| Type | Description |
|-|-|
| Array | Array created from the object. |
[Table 6. Returns]

{#r_AU-convertArray_O__table_n5s_mb1_4t}  
This example converts a Java object to an array.

    var arrayUtil = new ArrayUtil();
    // Returns a JavaObject with the logged in user's groups
    var groupObj = gs.getUser().getMyGroups();
    gs.print('groupObj: ' + Object.prototype.toString.call(groupObj));

    var groupArr = arrayUtil.convertArray(groupObj);
    gs.print('groupArr: ' + Object.prototype.toString.call(groupArr));

Output:

    groupObj: [object JavaObject]
    groupArr: [object Array]

## ArrayUtil - diff(Array a, Array b) {#ariaid-title5}

Finds the differences between two or more arrays.
Any number of arrays can be provided as parameters.
{#r_AU-diff_A_A__table_pgr_xb1_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| a | Array | An array |
| b | Array | An array |
[Table 7. Parameters]

{#r_AU-diff_A_A__table_pgr_xb1_4t} {#r_AU-diff_A_A__table_qgr_xb1_4t__entry__2}

| 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. |
[Table 8. Returns]

{#r_AU-diff_A_A__table_qgr_xb1_4t}  

    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) {#ariaid-title6}

Returns an array from the specified object.
{#r_AU-ensureArray_O__table_wh1_hxz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| object | Object | Object from which to create an array. |
[Table 9. Parameters]

{#r_AU-ensureArray_O__table_wh1_hxz_nt} {#r_AU-ensureArray_O__table_xh1_hxz_nt__entry__2}

| Type | Description |
|-|-|
| Array | Array created from the object. |
[Table 10. Returns]

{#r_AU-ensureArray_O__table_xh1_hxz_nt}  
The following example shows how to create an array from an object and display created
array.

    var arrayUtil = new ArrayUtil();
    var o1 = {a:"1",b:"2",c:"3"};
    gs.print('o1 is array: ' + Array.isArray(o1));
    gs.print('o1 stringified: ' + JSON.stringify(o1));

    var a1 = arrayUtil.ensureArray(o1);
    gs.print('a1 is array: ' + Array.isArray(a1));
    gs.print('a1 stringified: ' + JSON.stringify(a1));

Output:

    o1 is array: false
    o1 stringified: {"a":"1","b":"2","c":"3"}
    a1 is array: true
    a1 stringified: [{"a":"1","b":"2","c":"3"}]

The following example shows how to create an array from an object and display the contents
of the array.

    var stock = { 'name': 'Servicenow', 'sym': 'NOW' };

    var arr = new ArrayUtil();
    var stArray = arr.ensureArray(stock);

    gs.info("Name is " + stArray[0]['name']);
    gs.info("Symbol is " + stArray[0]['sym']);

Output:

    Name is Servicenow
    Symbol is NOW

## ArrayUtil - indexOf(Array array, Object element) {#ariaid-title7}

Searches the array for the element. Returns the element index or -1 if not
found.
{#r_AU-indexOf_A_O__table_igr_nwz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| array | Array | Array to search. |
| element | Object | Element to search for. |
[Table 11. Parameters]

{#r_AU-indexOf_A_O__table_igr_nwz_nt} {#r_AU-indexOf_A_O__table_jgr_nwz_nt__entry__2}

| Type | Description |
|-|-|
| Number | Position of the element in the array, or -1 if the element is not found. |
[Table 12. Returns]

{#r_AU-indexOf_A_O__table_jgr_nwz_nt}  

    var arrayUtil = new ArrayUtil();
    var arr = new Array("a", "b", "c", "x", "y", "z");
    gs.print("Array: " + arr);

    gs.print("Index of a: " + arrayUtil.indexOf(arr, "a"));
    gs.print("Index of a starting at 2: " + arrayUtil.indexOf(arr, "a", 2));

    gs.print("Index of c: " + arrayUtil.indexOf(arr, "c"));
    gs.print("Index of c starting at 1: " + arrayUtil.indexOf(arr, "c", 1));

    gs.print("Index of z: " + arrayUtil.indexOf(arr, "z"));
    gs.print("Index of z starting at 4: " + arrayUtil.indexOf(arr, "z", 4));

    // If negative value is sent as startIndex then (startIndex + length of array ) is considered as start index.

    // startIndex = -1+(6); startIndex is considered as 5 in this case
    gs.print("Index of c starting at -1 (Re-Calculated to 5): " + arrayUtil.indexOf(arr, "c", -1)); 

    // startIndex = -10+(6) which is -4,if negative value again then startIndex is considered as 0
    gs.print("Index of c starting at -10 (Re-Calculated to 0): " + arrayUtil.indexOf(arr, "c", -10)); 

Output:

    Array: a,b,c,x,y,z
    Index of a: 0
    Index of a starting at 2: -1
    Index of c: 2
    Index of c starting at 1: 2
    Index of z: 5
    Index of z starting at 4: 5
    Index of c starting at -1 (Re-Calculated to 5): -1
    Index of c starting at -10 (Re-Calculated to 0): 2

## ArrayUtil - indexOf(Array array, Object element, Number startIndex) {#ariaid-title8}

Searches the array for the element starting at the specified index. Returns the element
index or -1 if not found.
{#r_AU-indexOf_A_O_N__table_xmd_rvz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| array | Array | Array to search. |
| element | Object | Element to search for. |
| startIndex | Number | Index to start searching from. |
[Table 13. Parameters]

{#r_AU-indexOf_A_O_N__table_xmd_rvz_nt} {#r_AU-indexOf_A_O_N__table_ymd_rvz_nt__entry__2}

| Type | Description |
|-|-|
| Number | Position of the element in the array, or -1 if the element is not found. |
[Table 14. Returns]

{#r_AU-indexOf_A_O_N__table_ymd_rvz_nt}  

    var arrayUtil = new ArrayUtil();
    var arr = new Array("a", "b", "c", "x", "y", "z");
    gs.print("Array: " + arr);

    gs.print("Index of a: " + arrayUtil.indexOf(arr, "a"));
    gs.print("Index of a starting at 2: " + arrayUtil.indexOf(arr, "a", 2));

    gs.print("Index of c: " + arrayUtil.indexOf(arr, "c"));
    gs.print("Index of c starting at 1: " + arrayUtil.indexOf(arr, "c", 1));

    gs.print("Index of z: " + arrayUtil.indexOf(arr, "z"));
    gs.print("Index of z starting at 4: " + arrayUtil.indexOf(arr, "z", 4));

    // If negative value is sent as startIndex then (startIndex + length of array ) is considered as start index.

    // startIndex = -1+(6); startIndex is considered as 5 in this case
    gs.print("Index of c starting at -1 (Re-Calculated to 5): " + arrayUtil.indexOf(arr, "c", -1)); 

    // startIndex = -10+(6) which is -4,if negative value again then startIndex is considered as 0
    gs.print("Index of c starting at -10 (Re-Calculated to 0): " + arrayUtil.indexOf(arr, "c", -10)); 

Output:

    Array: a,b,c,x,y,z
    Index of a: 0
    Index of a starting at 2: -1
    Index of c: 2
    Index of c starting at 1: 2
    Index of z: 5
    Index of z starting at 4: 5
    Index of c starting at -1 (Re-Calculated to 5): -1
    Index of c starting at -10 (Re-Calculated to 0): 2

## ArrayUtil - intersect(Array a, Array b) {#ariaid-title9}

Finds the elements present in all arrays.
Any number of arrays can be provided as parameters.
{#r_AU-intersect_A_A__table_v5v_4c1_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| a | Array | An array |
| b | Array | An array |
[Table 15. Parameters]

{#r_AU-intersect_A_A__table_v5v_4c1_4t} {#r_AU-intersect_A_A__table_w5v_4c1_4t__entry__2}

| Type | Description |
|-|-|
| Array | An array of elements from array a that were found in all of the other input arrays. Duplicates are removed. |
[Table 16. Returns]

{#r_AU-intersect_A_A__table_w5v_4c1_4t}  

    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) {#ariaid-title10}

Merge two or more arrays.
Any number of arrays can be provided as parameters.
{#r_AU-union_A_A__table_xvq_2d1_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| a | Array | An array |
| b | Array | An array |
[Table 17. Parameters]

{#r_AU-union_A_A__table_xvq_2d1_4t} {#r_AU-union_A_A__table_yvq_2d1_4t__entry__2}

| Type | Description |
|-|-|
| Array | An array of items from all the input arrays. Duplicates are removed. |
[Table 18. Returns]

{#r_AU-union_A_A__table_yvq_2d1_4t}  

    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) {#ariaid-title11}

Removes duplicate items from an array.
{#r_AU-unique_A__table_zqw_td1_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| a | Array | The array to check for duplicate elements. |
[Table 19. Parameters]

{#r_AU-unique_A__table_zqw_td1_4t} {#r_AU-unique_A__table_arw_td1_4t__entry__2}

| Type | Description |
|-|-|
| Array | An array of unique items from the input array. |
[Table 20. Returns]

{#r_AU-unique_A__table_arw_td1_4t}  

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

Output: a,c,b

