ArrayUtil - Global
A API ArrayUtil é uma inclusão de script com funções úteis para trabalhar com matrizes JavaScript.
Esses métodos estão disponíveis para qualquer script do lado do servidor.
ArrayUtil - concat (matriz primária, matriz secundária)
Mesclar duas matrizes.
| Nome | Tipo | Descrição |
|---|---|---|
| primário | Matriz | Uma matriz a ser mesclada |
| secundário | Matriz | Uma matriz a ser mesclada |
| Tipo | Descrição |
|---|---|
| Matriz | Uma matriz de elementos de ambas as matrizes de entrada. Duplicatas não são removidas. |
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));
Saída: concat a1, a2: a,b,c,c,d,e
ArrayUtil - contains(matriz Array, elemento Object)
Pesquisa a matriz para o elemento especificado. Retorna verdadeiro se o elemento existir na matriz, caso contrário, retorna falso.
| Nome | Tipo | Descrição |
|---|---|---|
| matriz | Matriz | Matriz a ser pesquisada. |
| elemento | Objeto | Elemento a ser pesquisado. |
| Tipo | Descrição |
|---|---|
| Booliano | Sinalizador que indica se o elemento foi encontrado na matriz. Valores possíveis:
|
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)
Converte um objeto Java em uma matriz.
| Nome | Tipo | Descrição |
|---|---|---|
| a | Objeto | Objeto a ser convertido. |
| Tipo | Descrição |
|---|---|
| Matriz | Matriz criada a partir do objeto. |
Este exemplo converte um objeto Java em uma matriz.
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));
Saída
groupObj: [object JavaObject]
groupArr: [object Array]
ArrayUtil - diff(matriz a, matriz b)
Encontra as diferenças entre duas ou mais matrizes.
Qualquer número de matrizes pode ser fornecido como parâmetros.
| Nome | Tipo | Descrição |
|---|---|---|
| a | Matriz | Uma matriz |
| b | Matriz | Uma matriz |
| Tipo | Descrição |
|---|---|
| Matriz | Retorna uma matriz de itens da matriz a que não foram encontrados na matriz b ou c ou em outras matrizes de entrada. Duplicatas são removidas do resultado. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.diff(a1, a2));
Saída: a,b
ArrayUtil - garantirArray(objeto objeto)
Retorna uma matriz do objeto especificado.
| Nome | Tipo | Descrição |
|---|---|---|
| objeto | Objeto | Objeto a partir do qual uma matriz será criada. |
| Tipo | Descrição |
|---|---|
| Matriz | Matriz criada a partir do objeto. |
O exemplo a seguir mostra como criar uma matriz a partir de um objeto e exibir a matriz criada.
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"}]O exemplo a seguir mostra como criar uma matriz a partir de um objeto e exibir o conteúdo da matriz.
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(matriz de matriz, elemento de objeto)
Pesquisa o elemento na matriz. Retorna o índice do elemento ou -1 se não for encontrado.
| Nome | Tipo | Descrição |
|---|---|---|
| matriz | Matriz | Matriz a ser pesquisada. |
| elemento | Objeto | Elemento a ser pesquisado. |
| Tipo | Descrição |
|---|---|
| Número | Posição do elemento na matriz ou -1 se o elemento não for encontrado. |
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(matriz de matriz, elemento de objeto, número startIndex)
Pesquisa a matriz em busca do elemento que começa no índice especificado. Retorna o índice do elemento ou -1 se não for encontrado.
| Nome | Tipo | Descrição |
|---|---|---|
| matriz | Matriz | Matriz a ser pesquisada. |
| elemento | Objeto | Elemento a ser pesquisado. |
| startIndex | Número | Índice a partir do qual iniciar a pesquisa. |
| Tipo | Descrição |
|---|---|
| Número | Posição do elemento na matriz ou -1 se o elemento não for encontrado. |
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(Matriz a, Matriz b)
Encontra os elementos presentes em todas as matrizes.
Qualquer número de matrizes pode ser fornecido como parâmetros.
| Nome | Tipo | Descrição |
|---|---|---|
| a | Matriz | Uma matriz |
| b | Matriz | Uma matriz |
| Tipo | Descrição |
|---|---|
| Matriz | Uma matriz de elementos da matriz a que foram encontrados em todas as outras matrizes de entrada. Duplicatas são removidas. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.intersect(a1, a2));
Saída: c
ArrayUtil - union(Matriz a, Matriz b)
Mesclar duas ou mais matrizes.
Qualquer número de matrizes pode ser fornecido como parâmetros.
| Nome | Tipo | Descrição |
|---|---|---|
| a | Matriz | Uma matriz |
| b | Matriz | Uma matriz |
| Tipo | Descrição |
|---|---|
| Matriz | Uma matriz de itens de todas as matrizes de entrada. Duplicatas são removidas. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.union(a1, a2));
Saída: a,b,c,d,e
ArrayUtil - exclusivo (matriz a)
Remove itens duplicados de uma matriz.
| Nome | Tipo | Descrição |
|---|---|---|
| a | Matriz | A matriz para verificar se há elementos duplicados. |
| Tipo | Descrição |
|---|---|
| Matriz | Uma matriz de itens exclusivos da matriz de entrada. |
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c", "c", "b");
gs.print(arrayUtil.unique(a1));
Saída: a,c,b