Shamma Negi
Kilo Sage
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
12-20-2023
03:01 AM
Hi All,
Today I will cover about the useful functionality of ArrayUtil.
The ArrayUtil API is a script include with useful functions for working with JavaScript arrays.
These methods are available to any server-side script.
1. concat(Array parent, Array child)
Merge two arrays.
Example
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
2. contains(Array array, Object element)
Searches the array for the specified element. Returns true if the element exists in the array, otherwise returns false.
Example
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
3. indexOf(Array array, Object element)
Searches the array for the element. Returns the element index or -1 if not found.
Example
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));
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
Hope it helps.
I hope this article helpful. Please mark it as helpful and bookmark if you like it.
Regards,
Shamma Negi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
