---
sourceDocument: Référence de l’API Xanadu
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/fr-FR/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - fr-FR

ft:publication_title :

    - Référence de l’API Xanadu

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# ArrayUtil - Global

# ArrayUtil - Global {#ariaid-title1}

* Rversion finale: Xanadu
* 
* Mis à jour 1 août 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 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 n'importe quel script côté serveur.

## ArrayUtil - concat(Tableau parent, Tableau enfant) {#ariaid-title2}

Fusionner deux tableaux.
{#r_AU-concat_A_A__table_ny5_yxz_nt__entry__3}

| Nom | Type | Description |
|-|-|-|
| parent | Tableau | Un tableau à fusionner |
| enfant | Tableau | Un tableau à fusionner |
[Tableau 1. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Tableau d'éléments des deux tableaux d'entrée. Les doublons ne sont pas supprimés. |
[Tableau 2. Renvoie]

{#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));

Sortie : concaténer a1, a2 : a,b,c,c,d,e

## ArrayUtil - contains(Tableau, tableau, élément d'objet) {#ariaid-title3}

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.
{#r_AU-contains_A_O__table_zbc_w5z_nt__entry__3}

| Nom | Type | Description |
|-|-|-|
| tableau | Tableau | Tableau à rechercher. |
| élément | Objet | Élément à rechercher. |
[Tableau 3. Paramètres]

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

| Type | Description |
|-|-|
| Booléen | Marqueur indiquant si l'élément a été trouvé dans le tableau. Valeurs possibles : * true : élément trouvé dans le tableau. * false : élément introuvable dans le tableau. {#r_AU-contains_A_O__ul_k2b_r45_knb} |
[Tableau 4. Renvoie]

{#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"));

Sortie :

    Contains b: true
    Contains x: false

## ArrayUtil : convertArray(Object a) {#ariaid-title4}

Convertit un objet Java en tableau.
{#r_AU-convertArray_O__table_m5s_mb1_4t__entry__3}

| Nom | Type | Description |
|-|-|-|
| a | Objet | Objet à convertir. |
[Tableau 5. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Tableau créé à partir de l'objet. |
[Tableau 6. Renvoie]

{#r_AU-convertArray_O__table_n5s_mb1_4t}  
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) {#ariaid-title5}

Recherche les différences entre deux ou plusieurs tableaux.
Un nombre illimité de tableaux peut être fourni en tant que paramètres.
{#r_AU-diff_A_A__table_pgr_xb1_4t__entry__3}

| Nom | Type | Description |
|-|-|-|
| a | Tableau | Un tableau |
| b | Tableau | Un tableau |
[Tableau 7. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Renvoie un tableau d'éléments du tableau a qui n'ont été trouvés ni dans les tableaux b ou c, ni dans d'autres tableaux d'entrée. Les doublons sont supprimés du résultat. |
[Tableau 8. Renvoie]

{#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));

Sortie : a,b

## ArrayUtil : ensureArray(objet objet) {#ariaid-title6}

Renvoie un tableau à partir de l'objet spécifié.
{#r_AU-ensureArray_O__table_wh1_hxz_nt__entry__3}

| Nom | Type | Description |
|-|-|-|
| objet | Objet | Objet à partir duquel créer un tableau. |
[Tableau 9. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Tableau créé à partir de l'objet. |
[Tableau 10. Renvoie]

{#r_AU-ensureArray_O__table_xh1_hxz_nt}  
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, tableau, élément d'objet) {#ariaid-title7}

Recherche l'élément dans le tableau. Renvoie l'index de l'élément ou -1 s'il est introuvable.
{#r_AU-indexOf_A_O__table_igr_nwz_nt__entry__3}

| Nom | Type | Description |
|-|-|-|
| tableau | Tableau | Tableau à rechercher. |
| élément | Objet | Élément à rechercher. |
[Tableau 11. Paramètres]

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

| Type | Description |
|-|-|
| Numéro | Position de l'élément dans le tableau, ou -1 si l'élément n'est pas trouvé. |
[Tableau 12. Renvoie]

{#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)); 

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, nombre, startIndex) {#ariaid-title8}

Recherche l'élément dans le tableau à partir de l'index spécifié. Renvoie l'index de l'élément ou -1 s'il est introuvable.
{#r_AU-indexOf_A_O_N__table_xmd_rvz_nt__entry__3}

| Nom | Type | Description |
|-|-|-|
| tableau | Tableau | Tableau à rechercher. |
| élément | Objet | Élément à rechercher. |
| startIndex | Numéro | Index à partir duquel commencer la recherche. |
[Tableau 13. Paramètres]

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

| Type | Description |
|-|-|
| Numéro | Position de l'élément dans le tableau, ou -1 si l'élément n'est pas trouvé. |
[Tableau 14. Renvoie]

{#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)); 

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(Tableau a, Tableau b) {#ariaid-title9}

Recherche les éléments présents dans tous les tableaux.
Un nombre illimité de tableaux peut être fourni en tant que paramètres.
{#r_AU-intersect_A_A__table_v5v_4c1_4t__entry__3}

| Nom | Type | Description |
|-|-|-|
| a | Tableau | Un tableau |
| b | Tableau | Un tableau |
[Tableau 15. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Tableau d'éléments du tableau a trouvés dans tous les autres tableaux d'entrée. Les doublons sont supprimés. |
[Tableau 16. Renvoie]

{#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));

Sortie : c

## ArrayUtil : union(Tableau a, Tableau b) {#ariaid-title10}

Fusionner deux tableaux ou plus.
Un nombre illimité de tableaux peut être fourni en tant que paramètres.
{#r_AU-union_A_A__table_xvq_2d1_4t__entry__3}

| Nom | Type | Description |
|-|-|-|
| a | Tableau | Un tableau |
| b | Tableau | Un tableau |
[Tableau 17. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Tableau d'éléments de tous les tableaux d'entrée. Les doublons sont supprimés. |
[Tableau 18. Renvoie]

{#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));

Sortie : a,b,c,d,e

## ArrayUtil - unique(Array a) {#ariaid-title11}

Supprime les éléments en double d'un tableau.
{#r_AU-unique_A__table_zqw_td1_4t__entry__3}

| Nom | Type | Description |
|-|-|-|
| a | Tableau | Tableau permettant de vérifier la présence d'éléments en double. |
[Tableau 19. Paramètres]

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

| Type | Description |
|-|-|
| Tableau | Tableau d'éléments uniques du tableau d'entrée. |
[Tableau 20. Renvoie]

{#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));

Sortie : a,c,b

