{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":958,"path":"api-reference/server-api-reference/c_ArrayUtilAPI.md","title":"ArrayUtil - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# ArrayUtil- Global\n\nThe ArrayUtil script include provides methods for working with JavaScript arrays.\n\nThese methods are available to any server-side script.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## ArrayUtil - concat\\(Array parent, Array child\\)\n\nMerge two arrays.\n\n|Name|Type|Description|\n|----|----|-----------|\n|parent|Array|An array to merge|\n|child|Array|An array to merge|\n\n|Type|Description|\n|----|-----------|\n|Array|An array of elements from both input arrays. Duplicates are not removed.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar a1 = new Array(\"a\", \"b\", \"c\");\nvar a2 = new Array(\"c\", \"d\", \"e\");\n \ngs.print(\"concat a1, a2: \" + arrayUtil.concat(a1, a2));\n```\n\nOutput: concat a1, a2: a,b,c,c,d,e\n\n## ArrayUtil - contains\\(Array array, Object element\\)\n\nSearches the array for the specified element. Returns true if the element exists in the array, otherwise returns false.\n\n|Name|Type|Description|\n|----|----|-----------|\n|array|Array|Array to search.|\n|element|Object|Element to search for.|\n\n<table id=\"table_acc_w5z_nt\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag indicating whether the element was found in the array.Possible values:\n\n-   true: Element found in array.\n-   false: Element not found in array.\n\n</td></tr></tbody>\n</table>```\nvar arrayUtil = new ArrayUtil();\nvar a1 = new Array(\"a\", \"b\", \"c\");\n \ngs.print(\"Contains b: \" + arrayUtil.contains(a1, \"b\"));\ngs.print(\"Contains x: \" + arrayUtil.contains(a1, \"x\"));\n```\n\nOutput:\n\n```\nContains b: true\nContains x: false\n```\n\n## ArrayUtil - convertArray\\(Object a\\)\n\nConverts a Java object to an array.\n\n|Name|Type|Description|\n|----|----|-----------|\n|a|Object|Object to convert.|\n\n|Type|Description|\n|----|-----------|\n|Array|Array created from the object.|\n\nThis example converts a Java object to an array.\n\n```\nvar arrayUtil = new ArrayUtil();\n// Returns a JavaObject with the logged in user's groups\nvar groupObj = gs.getUser().getMyGroups();\ngs.print('groupObj: ' + Object.prototype.toString.call(groupObj));\n\nvar groupArr = arrayUtil.convertArray(groupObj);\ngs.print('groupArr: ' + Object.prototype.toString.call(groupArr));\n```\n\nOutput:\n\n```\ngroupObj: [object JavaObject]\ngroupArr: [object Array]\n```\n\n## ArrayUtil - diff\\(Array a, Array b\\)\n\nFinds the differences between two or more arrays.\n\nAny number of arrays can be provided as parameters.\n\n|Name|Type|Description|\n|----|----|-----------|\n|a|Array|An array|\n|b|Array|An array|\n\n|Type|Description|\n|----|-----------|\n|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.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar a1 = new Array(\"a\", \"b\", \"c\");\nvar a2 = new Array(\"c\", \"d\", \"e\");\ngs.print(arrayUtil.diff(a1, a2));\n```\n\nOutput: a,b\n\n## ArrayUtil - ensureArray\\(Object object\\)\n\nReturns an array from the specified object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|object|Object|Object from which to create an array.|\n\n|Type|Description|\n|----|-----------|\n|Array|Array created from the object.|\n\nThe following example shows how to create an array from an object and display created array.\n\n```\nvar arrayUtil = new ArrayUtil();\nvar o1 = {a:\"1\",b:\"2\",c:\"3\"};\ngs.print('o1 is array: ' + Array.isArray(o1));\ngs.print('o1 stringified: ' + JSON.stringify(o1));\n\nvar a1 = arrayUtil.ensureArray(o1);\ngs.print('a1 is array: ' + Array.isArray(a1));\ngs.print('a1 stringified: ' + JSON.stringify(a1));\n```\n\nOutput:\n\n```\no1 is array: false\no1 stringified: {\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}\na1 is array: true\na1 stringified: [{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}]\n```\n\nThe following example shows how to create an array from an object and display the contents of the array.\n\n```\nvar stock = { 'name': 'Servicenow', 'sym': 'NOW' };\n\nvar arr = new ArrayUtil();\nvar stArray = arr.ensureArray(stock);\n\ngs.info(\"Name is \" + stArray[0]['name']);\ngs.info(\"Symbol is \" + stArray[0]['sym']);\n```\n\nOutput:\n\n```\nName is Servicenow\nSymbol is NOW\n```\n\n## ArrayUtil - indexOf\\(Array array, Object element\\)\n\nSearches the array for the element. Returns the element index or -1 if not found.\n\n|Name|Type|Description|\n|----|----|-----------|\n|array|Array|Array to search.|\n|element|Object|Element to search for.|\n\n|Type|Description|\n|----|-----------|\n|Number|Position of the element in the array, or -1 if the element is not found.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar arr = new Array(\"a\", \"b\", \"c\", \"x\", \"y\", \"z\");\ngs.print(\"Array: \" + arr);\n\ngs.print(\"Index of a: \" + arrayUtil.indexOf(arr, \"a\"));\ngs.print(\"Index of a starting at 2: \" + arrayUtil.indexOf(arr, \"a\", 2));\n\ngs.print(\"Index of c: \" + arrayUtil.indexOf(arr, \"c\"));\ngs.print(\"Index of c starting at 1: \" + arrayUtil.indexOf(arr, \"c\", 1));\n\ngs.print(\"Index of z: \" + arrayUtil.indexOf(arr, \"z\"));\ngs.print(\"Index of z starting at 4: \" + arrayUtil.indexOf(arr, \"z\", 4));\n\n// If negative value is sent as startIndex then (startIndex + length of array ) is considered as start index.\n\n// startIndex = -1+(6); startIndex is considered as 5 in this case\ngs.print(\"Index of c starting at -1 (Re-Calculated to 5): \" + arrayUtil.indexOf(arr, \"c\", -1)); \n\n// startIndex = -10+(6) which is -4,if negative value again then startIndex is considered as 0\ngs.print(\"Index of c starting at -10 (Re-Calculated to 0): \" + arrayUtil.indexOf(arr, \"c\", -10)); \n```\n\nOutput:\n\n```\nArray: a,b,c,x,y,z\nIndex of a: 0\nIndex of a starting at 2: -1\nIndex of c: 2\nIndex of c starting at 1: 2\nIndex of z: 5\nIndex of z starting at 4: 5\nIndex of c starting at -1 (Re-Calculated to 5): -1\nIndex of c starting at -10 (Re-Calculated to 0): 2\n```\n\n## ArrayUtil - indexOf\\(Array array, Object element, Number startIndex\\)\n\nSearches the array for the element starting at the specified index. Returns the element index or -1 if not found.\n\n|Name|Type|Description|\n|----|----|-----------|\n|array|Array|Array to search.|\n|element|Object|Element to search for.|\n|startIndex|Number|Index to start searching from.|\n\n|Type|Description|\n|----|-----------|\n|Number|Position of the element in the array, or -1 if the element is not found.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar arr = new Array(\"a\", \"b\", \"c\", \"x\", \"y\", \"z\");\ngs.print(\"Array: \" + arr);\n\ngs.print(\"Index of a: \" + arrayUtil.indexOf(arr, \"a\"));\ngs.print(\"Index of a starting at 2: \" + arrayUtil.indexOf(arr, \"a\", 2));\n\ngs.print(\"Index of c: \" + arrayUtil.indexOf(arr, \"c\"));\ngs.print(\"Index of c starting at 1: \" + arrayUtil.indexOf(arr, \"c\", 1));\n\ngs.print(\"Index of z: \" + arrayUtil.indexOf(arr, \"z\"));\ngs.print(\"Index of z starting at 4: \" + arrayUtil.indexOf(arr, \"z\", 4));\n\n// If negative value is sent as startIndex then (startIndex + length of array ) is considered as start index.\n\n// startIndex = -1+(6); startIndex is considered as 5 in this case\ngs.print(\"Index of c starting at -1 (Re-Calculated to 5): \" + arrayUtil.indexOf(arr, \"c\", -1)); \n\n// startIndex = -10+(6) which is -4,if negative value again then startIndex is considered as 0\ngs.print(\"Index of c starting at -10 (Re-Calculated to 0): \" + arrayUtil.indexOf(arr, \"c\", -10)); \n```\n\nOutput:\n\n```\nArray: a,b,c,x,y,z\nIndex of a: 0\nIndex of a starting at 2: -1\nIndex of c: 2\nIndex of c starting at 1: 2\nIndex of z: 5\nIndex of z starting at 4: 5\nIndex of c starting at -1 (Re-Calculated to 5): -1\nIndex of c starting at -10 (Re-Calculated to 0): 2\n```\n\n## ArrayUtil - intersect\\(Array a, Array b\\)\n\nFinds the elements present in all arrays.\n\nAny number of arrays can be provided as parameters.\n\n|Name|Type|Description|\n|----|----|-----------|\n|a|Array|An array|\n|b|Array|An array|\n\n|Type|Description|\n|----|-----------|\n|Array|An array of elements from array a that were found in all of the other input arrays. Duplicates are removed.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar a1 = new Array(\"a\", \"b\", \"c\");\nvar a2 = new Array(\"c\", \"d\", \"e\");\ngs.print(arrayUtil.intersect(a1, a2));\n```\n\nOutput: c\n\n## ArrayUtil - union\\(Array a, Array b\\)\n\nMerge two or more arrays.\n\nAny number of arrays can be provided as parameters.\n\n|Name|Type|Description|\n|----|----|-----------|\n|a|Array|An array|\n|b|Array|An array|\n\n|Type|Description|\n|----|-----------|\n|Array|An array of items from all the input arrays. Duplicates are removed.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar a1 = new Array(\"a\", \"b\", \"c\");\nvar a2 = new Array(\"c\", \"d\", \"e\");\ngs.print(arrayUtil.union(a1, a2));\n```\n\nOutput: a,b,c,d,e\n\n## ArrayUtil - unique\\(Array a\\)\n\nRemoves duplicate items from an array.\n\n|Name|Type|Description|\n|----|----|-----------|\n|a|Array|The array to check for duplicate elements.|\n\n|Type|Description|\n|----|-----------|\n|Array|An array of unique items from the input array.|\n\n```\nvar arrayUtil = new ArrayUtil();\nvar a1 = new Array(\"a\", \"b\", \"c\", \"c\", \"b\");\ngs.print(arrayUtil.unique(a1));\n```\n\nOutput: a,c,b\n\n"}}