ArrayUtil - Global
L’API ArrayUtil est un script include avec des fonctions utiles pour travailler avec des tableaux JavaScript.
Ces méthodes sont disponibles pour n’importe quel script côté serveur.
ArrayUtil - concat(Tableau parent, Tableau enfant)
Fusionner deux tableaux.
| Nom | Type | Description |
|---|---|---|
| parent | Tableau | Un tableau à fusionner |
| enfant | Tableau | Un tableau à fusionner |
| Type | Description |
|---|---|
| Tableau | Tableau d’éléments provenant des deux tableaux d’entrée. Les doublons ne sont pas supprimés. |
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));
Sortie : concaténation a1, a2 : a,b,c,c,d,e
ArrayUtil - contains(Tableau tableau, Élément d’objet)
Recherche l’élément spécifié dans le tableau. Renvoie la valeur true si l’élément existe dans le tableau, sinon renvoie la valeur false.
| Nom | Type | Description |
|---|---|---|
| tableau | Tableau | Tableau à rechercher. |
| élément | Objet | Élément à rechercher. |
| Type | Description |
|---|---|
| Booléen | Marqueur indiquant si l’élément a été trouvé dans le tableau. Valeurs possibles :
|
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"));
Contains b: true
Contains x: falseArrayUtil - convertArray(Object a)
Convertit un objet Java en tableau.
| Nom | Type | Description |
|---|---|---|
| a | Objet | Objet à convertir. |
| Type | Description |
|---|---|
| Tableau | Tableau créé à partir de l’objet. |
Cet exemple convertit un objet Java en tableau.
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));
Sortie
groupObj: [object JavaObject]
groupArr: [object Array]
ArrayUtil - diff(Tableau a, Tableau b)
Recherche les différences entre deux tableaux ou plus.
Un nombre illimité de tableaux peut être fourni en tant que paramètres.
| Nom | Type | Description |
|---|---|---|
| a | Tableau | Un tableau |
| b | Tableau | Un tableau |
| Type | Description |
|---|---|
| Tableau | Renvoie un tableau d’éléments du tableau a qui n’ont pas été trouvés dans les tableaux b ou c, ou dans d’autres tableaux d’entrée. Les doublons sont supprimés du résultat. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.diff(a1, a2));
Sortie : a,b
ArrayUtil - ensureArray(objet objet)
Renvoie un tableau à partir de l’objet spécifié.
| Nom | Type | Description |
|---|---|---|
| objet | Objet | Objet à partir duquel créer un tableau. |
| Type | Description |
|---|---|
| Tableau | Tableau créé à partir de l’objet. |
L’exemple suivant montre comment créer un tableau à partir d’un objet et afficher le tableau créé.
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));
o1 is array: false
o1 stringified: {"a":"1","b":"2","c":"3"}
a1 is array: true
a1 stringified: [{"a":"1","b":"2","c":"3"}]L’exemple suivant montre comment créer un tableau à partir d’un objet et afficher le contenu du tableau.
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']);
Name is Servicenow
Symbol is NOWArrayUtil - indexOf(Tableau, tableau, élément d’objet)
Recherche l’élément dans le tableau. Renvoie l’élément index ou -1 s’il est introuvable.
| Nom | Type | Description |
|---|---|---|
| tableau | Tableau | Tableau à rechercher. |
| élément | Objet | Élément à rechercher. |
| Type | Description |
|---|---|
| Numéro | Position de l’élément dans le tableau, ou -1 si l’élément est introuvable. |
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));
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): 2ArrayUtil - indexOf(Tableau tableau, Élément d’objet, Nombre startIndex)
Recherche dans le tableau l’élément à partir de l’index spécifié. Renvoie l’élément index ou -1 s’il est introuvable.
| Nom | Type | Description |
|---|---|---|
| tableau | Tableau | Tableau à rechercher. |
| élément | Objet | Élément à rechercher. |
| startIndex | Numéro | Index à partir duquel démarrer la recherche. |
| Type | Description |
|---|---|
| Numéro | Position de l’élément dans le tableau, ou -1 si l’élément est introuvable. |
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));
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): 2ArrayUtil - intersect(Tableau a, Tableau b)
Trouve les éléments présents dans tous les tableaux.
Un nombre illimité de tableaux peut être fourni en tant que paramètres.
| Nom | Type | Description |
|---|---|---|
| a | Tableau | Un tableau |
| b | Tableau | Un tableau |
| Type | Description |
|---|---|
| Tableau | Tableau d’éléments du tableau a qui ont été trouvés dans tous les autres tableaux d’entrée. Les doublons sont supprimés. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.intersect(a1, a2));
Sortie : c
ArrayUtil - union(Tableau a, Tableau b)
Fusionner deux tableaux ou plus.
Un nombre illimité de tableaux peut être fourni en tant que paramètres.
| Nom | Type | Description |
|---|---|---|
| a | Tableau | Un tableau |
| b | Tableau | Un tableau |
| Type | Description |
|---|---|
| Tableau | Tableau d’éléments de tous les tableaux d’entrée. Les doublons sont supprimés. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.union(a1, a2));
Sortie : a,b,c,d,e
ArrayUtil - unique(Array a)
Supprime les éléments en double d’un tableau.
| Nom | Type | Description |
|---|---|---|
| a | Tableau | Tableau permettant de rechercher les éléments en double. |
| Type | Description |
|---|---|
| Tableau | Tableau d’éléments uniques du tableau d’entrée. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c", "c", "b");
gs.print(arrayUtil.unique(a1));
Sortie : a,c,b