Array Collection
Overview
Array collection related functions
of
Convert multiple elements to an array
Array.of(...item);| Parameter | Type | Description |
|---|---|---|
| item | Object | Object |
Return Value
Type Array<Object> Returns an array
Examples
Array.of(); //[]
Array.of("x", 2, 3, 4); //["x",2,3,4]
Array.of("x", null, 3, 4); //["x",null,3,4]toList
Convert elements to an array
Array.toList(item);| Parameter | Type | Description |
|---|---|---|
| item | Object | Object, returns a clone if item is already an array |
Return Value
Type Array<Object> Returns an array
Examples
Array.toList(); //null
Array.toList("x", 2, 3, 4); //["x",2,3,4]
Array.toList(["x", 2, 3, 4]); //["x",2,3,4]
Array.toList("x", null, 3, 4); //["x",null,3,4]
Array.toList("x"); //["x"]
Array.toList(null); //[null]join
Join the contents of the seq array together using separator
Array.join(array, separator);| Parameter | Type | Description |
|---|---|---|
| array | Array | Array to join |
| separator | String | Separator |
Return Value
Type String Returns the joined string
Examples
Array.join([1, 2, 3, 4], "-"); //"1-2-3-4"
Array.join([1, 2, 3, 4], null); //"1234"
Array.join([1, null, 3, 4], null); //"134"
Array.join([null, null, null, null], null); //""
Array.join(null, "-"); //null
Array.join(null, null); //nulllength
Number of elements in the list
Array.length(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Array to get length of |
Return Value
Type Integer Returns the length of the array, returns 0 if the array is null
Examples
Array.length([1, 2, 3, 4]); //4
Array.length([1, null, 3, 4]); //4
Array.length(null); //0get
Get an element from the collection
If it's a List collection, the key is an integer index from 0 to (len - 1), returns the value at that position, accessing beyond the range will throw an exception; if it's a Map collection, the key is the key of the key-value pair, returns the corresponding value;
Array.get(list, key);| Parameter | Type | Description |
|---|---|---|
| list | Array or Map | Collection to get element from |
| key | Integer or String | Element index or key value |
Return Value
Type Object Returns the element in the collection, returns null if the element doesn't exist
Examples
Array.get([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], 1); //{name=Li Si}
Array.get([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], -1); //null
Array.get([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], 3); //null
Array.get([{ name: "Zhang San" }, null, { name: "Wang Wu" }], 2); //{name=Wang Wu}
Array.get({ name: "Zhang San" }, "name"); //Zhang San
Array.get({ name: "Zhang San" }, "age"); //null
Array.get(null, 3); //nullset
Set the element value in the collection. If it's a List collection, replace the item with key as value; if it's a Map collection, the key is the key of the key-value pair
Array.set(list, key, value);| Parameter | Type | Description | | --------- | ---------------- | ---------------------------- | ----------------------- | | list | Array or Map | Collection to set element in | | key | String | Integer | Property value or index | | value | Object | Value to set |
Return Value
Type Array|Map Returns the modified collection
Examples
Array.set({ name: "Zhang San" }, "name", "Zhang San San"); //{"name":"Zhang San San"}
Array.set(
[
{ name: "Zhang San", age: 18 },
{ name: "Zhang San", age: 24 },
],
"age",
20
); //[{ 'name': 'Zhang San', "age": 20 }, { 'name': 'Zhang San', "age": 20 }]
Array.set(
[
{ name: "Zhang San", age: 18 },
{ name: "Zhang San", age: 24 },
],
1,
20
); //[{ 'name': 'Zhang San', "age": 20 }, 20]add
Add element item to the end of collection list
Array.add(list, item);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection to add element to |
| item | Object | Element to add |
Return Value
Type Array Returns the collection after adding the element
Examples
Array.add([1, 2, 3, 4, 5, 6], { name: "Li Si" }); //[1,2,3,4,5,6,{"name":"Li Si"}]
Array.add([1, 2, 3, 4, 5, 6], 12); //[1,2,3,4,5,6,12]
Array.add(null, { name: "Li Si" }); //nullremove
Remove the element from the collection, if the collection is of Map type, item is the key
Array.remove(list, item);| Parameter | Type | Description |
|---|---|---|
| list | Array or Map | Collection to remove element from |
| item | Object or String | Element to remove, element's key |
Return Value
Type Array or Map Returns the collection after removing the element
Examples
Array.remove([1, 2, 3, 4, 5, 6], { name: "Li Si" }); //[1,2,3,4,5,6]
Array.remove([1, 2, 3, 4, 5, 6], 5); //[1,2,3,4,6]
Array.remove({ name: "Li Si", age: 30 }, "name"); //{"age":30}
Array.remove({ name: "Li Si", age: 30 }, "sex"); //{"name":"Li Si","age":30}
Array.remove(null, { name: "Li Si" }); //null
Array.remove([null, 1, 2, 3, null], null); //[1,2,3,null]Note
If list is of type Array, remove only deletes the first matching element when removing internal elements
removeAll
Remove elements from the collection, if the collection is of Map type, item is the key
Array.removeAll(list, item);| Parameter | Type | Description |
|---|---|---|
| list | Array or Map | Collection to remove elements from |
| item | Object or String or Array | Element to remove, element's key, array of elements |
Return Value
Type Array or Map Returns the collection after removing the elements
Examples
Array.removeAll([1, 2, 3, 4, 5, 6], { name: "Li Si" }); //[1,2,3,4,5,6]
Array.removeAll([1, 2, 3, 4, 5, 6], [1, 5]); //[2,3,4,6]
Array.removeAll({ name: "Li Si", age: 30 }, "name"); //{"age":30}
Array.removeAll({ name: "Li Si", age: 30 }, "sex"); //{"name":"Li Si","age":30}
Array.removeAll(null, { name: "Li Si" }); //null
Array.removeAll([null, 1, 2, 3, null], null); //[1,2,3]
Array.removeAll([null, 1, 2, 3, null], [null, 1]); //[2,3]Note
If list is of type Array, remove only deletes the first matching element when removing internal elements
isEmpty
Check if the list is an empty collection; returns true if the list is null or has a length of 0, otherwise returns false
Array.isEmpty(list);| Parameter | Type | Description |
|---|---|---|
| list | Array or Map | Collection to check length |
Return Value
Type Boolean Whether the collection is empty
Examples
Array.isEmpty([1, 2, 3, 4, 5, 6]); //false
Array.isEmpty(0); //false
Array.isEmpty(1); //false
Array.isEmpty(0.0); //false
Array.isEmpty(1.0); //false
Array.isEmpty(null); //true
Array.isEmpty({ name: "Li Si", age: 30 }); //false
Array.isEmpty({}); //true
Array.isEmpty(""); //trueisNotEmpty
Check if the list is not an empty collection; returns false if the list is null or has a length of 0, otherwise returns true
Array.isNotEmpty(list);| Parameter | Type | Description |
|---|---|---|
| list | Array or Map | Collection to check length |
Return Value
Type Boolean Whether the collection is not empty
Examples
Array.isNotEmpty([1, 2, 3, 4, 5, 6]); //true
Array.isNotEmpty(0); //true
Array.isNotEmpty(1); //true
Array.isNotEmpty(0.0); //true
Array.isNotEmpty(1.0); //true
Array.isNotEmpty(null); //false
Array.isNotEmpty({ name: "Li Si", age: 30 }); //true
Array.isNotEmpty({}); //false
Array.isNotEmpty(""); //falsecontains
Check if the list contains the item
Array.contains(list, item);| Parameter | Type | Description |
|---|---|---|
| list | Array or Map | Collection |
| item | Object | Element |
Return Value
Type Boolean Whether the collection Array or Map contains the item
Examples
Array.contains([1, 2, 3, 4, 5, 6], 1); //true
Array.contains([2, 3, 4, 5, 6], 1); //false
Array.contains(["1", 2, 3, 4, 5, 6], 1); //false
Array.contains([{ name: "Zhang San" }, { name: "Li Si" }], { name: "Zhang San" }); //true
Array.contains(null, "1"); //falsecontainsAny
Check if list1 contains any element from list2
Always returns false when list1 is null, always returns true when list2 is empty
Array.containsAny(list1, list2);| Parameter | Type | Description |
|---|---|---|
| list1 | Array | Collection 1 |
| list2 | Array | Collection 2 |
Return Value
Type Boolean Whether list1 contains any element from list2
Examples
Array.containsAny([1, 2, 3, 4, 5, 6], [1]); //true
Array.containsAny([1, 2, 3, 4, 5, 6], [8]); //false
Array.containsAny([1, 2, 3, 4, 5, 6], ["1"]); //false
Array.containsAny([1, 2, 3, 4, 5, 6], null); //true
Array.containsAny(null, ["1"]); //false
Array.containsAny(null, null); //falsecontainsAll
Check if list1 contains all elements from list2
Always returns false when list1 or list2 is empty
Array.containsAll(list1, list2);| Parameter | Type | Description |
|---|---|---|
| list1 | Array | Collection 1 |
| list2 | Array | Collection 2 |
Return Value
Type Boolean Whether list1 contains all elements from list2
Examples
Array.containsAll([1, 2, 3], [2, 3]); //true
Array.containsAll([1, 2, 3], [2, 3, 4]); //false
Array.containsAll([1, 2, 3], ["1", 2]); //false
Array.containsAll([1, 2, 3], [1, 2, 3, 4]); //false
Array.containsAll(null, ["1"]); //false
Array.containsAll(null, null); //falsemap
Return a list of the key attributes in the list
Array.map(list, key);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection 1 |
| key | String | Attribute key |
Return Value
Type Array List of each element's key attribute in the list
Examples
Array.map([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], "name"); //["Zhang San","Li Si","Wang Wu"]
Array.map([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], "age"); //[null,null,null]
Array.map([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], null); //[]
Array.map([null, null, { name: "Zhang San" }], "name"); //[null,null,"Zhang San"]
Array.map(null, "name"); //[]props
Return a new list of objects composed of the elements' props list in the list
Array.props(list, props);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection 1 |
| props | Array<String> | Attribute key list, throws exception when props is null |
Return Value
Type Array New list constructed from each element's props list attributes in the list
Examples
Array.props(
[
{ name: "Zhang San", age: 1 },
{ name: "Li Si", age: 2 },
{ name: "Wang Wu", age: 3 },
],
"name"
); //[{'name':'Zhang San'},{'name':'Li Si'},{'name':'Wang Wu'}]
Array.props([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], "age"); //[{'age':null},{'age':null},{'age':null}]
Array.props([{ name: "Zhang San" }, { name: "Li Si" }, { name: "Wang Wu" }], null); //exception case
Array.props([null, null, { name: "Zhang San" }], "name"); //[{'name':'Zhang San'}]
Array.props(null, "name"); //[]transform
Return a new list of objects composed of the elements' mapping according to the mapping in the list
Array.transform(list, mapping);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection 1 |
| mapping | Map<String,String> | Attribute mapping object |
Return Value
Returns a new list of objects composed of the elements' mapping according to the mapping in the list
Examples
Array.transform(
[
{ name: "Zhang San", age: 1 },
{ name: "Li Si", age: 2 },
{ name: "Wang Wu", age: 3 },
],
{ name: "userName", age: "userAge" }
); //[{'userName':'Zhang San','userAge':1},{'userName':'Li Si','userAge':2},{'userName':'Wang Wu','userAge':3}]
Array.transform(
[
{ name: "Zhang San", age: 1 },
{ name: "Li Si", age: 2 },
{ name: "Wang Wu", age: 3 },
],
{ name: "userName", sex: "userSex" }
); //[{'userName':'Zhang San','userSex':null},{'userName':'Li Si','userSex':null},{'userName':'Wang Wu','userSex':null}]
Array.transform([{ name: "Zhang San", age: 1 }, { name: "Li Si", age: 2 }, null], { name: "userName", age: "userAge" }); //[{'userName':'Zhang San','userAge':1},{'userName':'Li Si','userAge':2}]
Array.transform(
[
{ name: "Zhang San", age: 1 },
{ name: "Li Si", age: 2 },
],
null
); //[]concat
Return list1 and list2 concatenated together
Array.concat(list1, list2);| Parameter | Type | Description |
|---|---|---|
| list1 | Array | Collection 1 |
| list2 | Array | Collection 2 |
Return Value
Type Array Collection after concatenating list1 and list2
Examples
Array.concat([1, 2, 3], [2, 3]); //[1,2,3,2,3]
Array.concat([1, 2, 3], [2, 3, 4]); //[1,2,3,2,3,4]
Array.concat([1, 2, 3], ["1", 2]); //[1,2,3,"1",2]
Array.concat([1, 2, 3], [1, 2, 3, 4]); //[1,2,3,1,2,3,4]
Array.concat([1, 2, 3], null); //[1,2,3]
Array.concat(null, ["1"]); //["1"]
Array.concat(null, null); //[]sort
Sort the list according to the key attribute
Array.sort(list, key);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
| key | String | Sorting attribute |
Return Value
Type Array Sorted collection
Examples
Array.sort(
[
{ name: "a", age: 20 },
{ name: "b", age: 18 },
{ name: "c", age: 32 },
{ name: "c", age: 24 },
],
"age"
); //[{"name":"a","age":20},{"name":"b","age":18},{"name":"c","age":32},{"name":"c","age":24}]
Array.sort(
[
{ male: true, age: 20 },
{ male: false, age: 18 },
{ male: true, age: 32 },
],
"male"
); //[{"male":true,"age":20},{"male":false,"age":18},{"male":true,"age":32}]
Array.sort(
[
{ male: true, age: 20 },
{ male: false, age: 18 },
],
null
); //[{"male":true,"age":20},{"male":false,"age":18}]
Array.sort(null, "age"); //null
Array.sort([5, 4, 9, 20, 32, 1, 244]); //[1,4,5,9,20,32,244]
Array.sort(null, null); //nulldistinct
Return the elements in the list with duplicates removed
Array.distinct(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Array Collection after removing duplicates
Examples
Array.distinct([
{ name: "a", age: 20 },
{ name: "a", age: 20 },
{ name: "c", age: 32 },
]); //[{"name":"a","age":20},{"name":"c","age":32}]
Array.distinct([1, 2, 2, 3, 4, 5, 67, 9]); //[1,2,3,4,5,67,9]
Array.distinct([1, 2, null, 3, 4, 5, null, 9]); //[1,2,null,3,4,5,9]
Array.distinct(null); //nullreverse
Return the list reversed
Array.reverse(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Array Reversed collection
Examples
Array.reverse([
{ name: "a", age: 20 },
{ name: "c", age: 32 },
]); //[{"name":"c","age":32},{"name":"a","age":20}]
Array.reverse([1, 2, 3, 4, 5, 6, 7, 9]); //[9,7,6,5,4,3,2,1]
Array.reverse([1, 2, null, 3, 4, 5, null, 9]); //[9,null,5,4,3,null,2,1]
Array.reverse(null); //nullrepeat
Generate an array with each item as element and length count
Array.repeat(count, element);| Parameter | Type | Description |
|---|---|---|
| count | Integer | Repeat times |
| element | Object | Element to repeat |
Return Value
Type Array Generated array
Examples
Array.repeat(4, { name: "a" }); //[{"name":"a"},{"name":"a"},{"name":"a"},{"name":"a"}]
Array.repeat(4, 1); //[1,1,1,1]
Array.repeat(3, "a"); //["a","a","a"]
Array.repeat(3, null); //[null,null,null]sublist
Return the part of the list from fromIndex to toIndex
Array.sublist(list, fromIndex, toIndex);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
| fromIndex | Integer | Start index, starts from 0 if null |
| endIndex | Integer | End index, position of the last element in the list if null |
Return Value
Type Array Generated array, returns null if the list is empty
Examples
Array.sublist([1, 2, 3, 4], 0, 1); //[1,2]
Array.sublist([1, 2, 3, 4], 0, 100); //[1,2,3,4]
Array.sublist([1, 2, 3, 4], 1, 1); //[2]
Array.sublist([1, 2, 3, 4], null, null); //[]
Array.sublist([1, 2, 3, 4], null, 2); //[1,2,3]
Array.sublist([1, 2, 3, 4], 1, null); //[2,3,4]
Array.sublist(null, 0, 1); //nullfilter
Filter items from the list where the value of the item's property key equals value
Array.filter(list, key, value);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
| key | String | Property key to compare |
| value | Object | Property value to compare |
Return Value
Type Array Array of items that meet the filtering condition
Examples
Array.filter([{ a: 1 }, { a: 2 }, { a: 2 }], "a", 2); //[{"a":2},{"a":2}]
Array.filter(null, "a", 2); //[]
Array.filter([{ a: 1 }, { a: 2 }, { a: 2 }], null, 2); //[]shift
Remove the first element from the list and return the removed element
Array.shift(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Object First element
Examples
Array.shift([1, 2, 3]); //1
Array.shift([]); //null
Array.shift(null); //nullpop
Remove the last element from the list and return the removed element
Array.pop(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Object Last element
Examples
Array.pop([1, 2, 3]); //3
Array.pop([]); //null
Array.pop(null); //nullsum
Sum the elements in a list of numeric types
Array.sum(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Double Sum result
Examples
Array.sum([1, 2, 3]); //6.0
Array.sum([]); //0.0
Array.sum([null, null, null]); //0.0
Array.sum([1, null, 2]); //3.0
Array.sum(null); //0.0
Array.sum(["1", "2", "3"]); //6.0 If array element type is string, the system will convert to decimal; returns 0 if conversion fails
Array.sum(["1", "2", "s"]); //3.0 If array element type is string, treated as 0 if conversion failsavg
Calculate the average of elements in a list of numeric types
Array.avg(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Double Average result
Examples
Array.avg([1, 2, 3]); //2.0
Array.avg([]); //0.0
Array.avg([null, null, null]); //0.0
Array.avg([1, null, 2]); //1.0
Array.avg(null); //0.0
Array.avg(["1", "2", "3"]); //2.0 If array element type is string, the system will convert to decimal; returns 0 if conversion fails
Array.avg(["1", "2", "s"]); //1.0 If array element type is string, treated as 0 if conversion failsmax
Calculate the maximum value of elements in a list of numeric types
Array.max(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Double Maximum value in the list
Examples
Array.max([1, 2, 3]); //3.0
Array.max([]); //null
Array.max([null, null, null]); //null
Array.max([1, null, 2]); //2.0
Array.max(null); //null
Array.max(["1", "2", "3"]); //3.0 If array element type is string, the system will convert to decimal; returns 0 if conversion fails
Array.max(["1", "2", "s"]); //2.0 If array element type is string, treated as 0 if conversion failsmin
Calculate the minimum value of elements in a list of numeric types
Array.min(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Double Minimum value in the list
Examples
Array.min([1, 2, 3]); //1.0
Array.min([]); //null
Array.min([null, null, null]); //null
Array.min([1, null, 2]); //1.0
Array.min(null); //null
Array.min(["1", "2", "3"]); //1.0 If array element type is string, the system will convert to decimal; returns 0 if conversion fails
Array.min(["1", "2", "s"]); //1.0 If array element type is string, ignored if conversion failsfirst
Return the first element in the list
Array.first(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Object First element in the list
Examples
Array.first([1, 2, 3]); //1
Array.first([]); //null
Array.first(null); //nulllast
Return the last element in the list
Array.last(list);| Parameter | Type | Description |
|---|---|---|
| list | Array | Collection |
Return Value
Type Object Last element in the list
Examples
Array.last([1, 2, 3]); //3
Array.last([]); //null
Array.last(null); //null
