ArrayUtil : global

  • Rversion finale: Yokohama
  • Mis à jour 30 janv. 2025
  • 6 minutes de lecture
  • L’include de script ArrayUtil fournit des méthodes pour travailler avec des tableaux JavaScript.

    Ces méthodes sont disponibles pour tout script côté serveur.

    ArrayUtil - concat(Array parent, Array child)

    Fusionner deux tableaux.

    Tableau 1. Paramètres
    Nom Type Description
    parent Tableau Un tableau à fusionner
    enfant Tableau Un tableau à fusionner
    Tableau 2. Renvoie
    Type Description
    Tableau Un 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éner a1, a2 : a, b, c, c, d, e

    ArrayUtil - contient(Tableau tableau, élément objet)

    Recherche l’élément spécifié dans le tableau. Renvoie la valeur vrai si l’élément existe dans le tableau, sinon renvoie la valeur faux.

    Tableau 3. Paramètres
    Nom Type Description
    tableau Tableau Tableau à rechercher.
    élément Objet Élément à rechercher.
    Tableau 4. Renvoie
    Type Description
    Booléen Marqueur indiquant si l’élément a été trouvé dans le tableau.
    Valeurs possibles :
    • vrai : élément trouvé dans le tableau.
    • false : élément introuvable dans le tableau.
    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"));
    Sortie :
    Contains b: true
    Contains x: false

    ArrayUtil - convertArray(Objet a)

    Convertit un objet Java en tableau.

    Tableau 5. Paramètres
    Nom Type Description
    a Objet Objet à convertir.
    Tableau 6. Renvoie
    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.

    N’importe quel nombre de tableaux peut être fourni en tant que paramètres.

    Tableau 7. Paramètres
    Nom Type Description
    a Tableau Un tableau
    b Tableau Un tableau
    Tableau 8. Renvoie
    Type Description
    Tableau Renvoie un tableau d’éléments du tableau a qui n’ont pas été trouvés dans le tableau 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é.

    Tableau 9. Paramètres
    Nom Type Description
    objet Objet Objet à partir duquel créer un tableau.
    Tableau 10. Renvoie
    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));
    Sortie :
    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']);
    Sortie :
    Name is Servicenow
    Symbol is NOW

    ArrayUtil : indexOf(tableau de tableau, élément d’objet)

    Recherche l’élément dans le tableau. Renvoie l’index de l’élément ou -1 si introuvable.

    Tableau 11. Paramètres
    Nom Type Description
    tableau Tableau Tableau à rechercher.
    élément Objet Élément à rechercher.
    Tableau 12. Renvoie
    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)); 
    Sortie :
    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(Tableau tableau, élément d’objet, nombre, startIndex)

    Recherche l’élément dans le tableau à partir de l’index spécifié. Renvoie l’index de l’élément ou -1 si introuvable.

    Tableau 13. Paramètres
    Nom Type Description
    tableau Tableau Tableau à rechercher.
    élément Objet Élément à rechercher.
    startIndex Numéro Index à partir duquel commencer la recherche.
    Tableau 14. Renvoie
    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)); 
    Sortie :
    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)

    Recherche les éléments présents dans tous les tableaux.

    N’importe quel nombre de tableaux peut être fourni en tant que paramètres.

    Tableau 15. Paramètres
    Nom Type Description
    a Tableau Un tableau
    b Tableau Un tableau
    Tableau 16. Renvoie
    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)

    Fusionnez deux tableaux ou plus.

    N’importe quel nombre de tableaux peut être fourni en tant que paramètres.

    Tableau 17. Paramètres
    Nom Type Description
    a Tableau Un tableau
    b Tableau Un tableau
    Tableau 18. Renvoie
    Type Description
    Tableau Tableau d’éléments provenant 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(Tableau a)

    Supprime les éléments en double d’un tableau.

    Tableau 19. Paramètres
    Nom Type Description
    a Tableau Tableau pour vérifier les éléments en double.
    Tableau 20. Renvoie
    Type Description
    Tableau Tableau d’éléments uniques à partir 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