Skip to content

Array Collection

Overview

Array collection related functions

of

Convert multiple elements to an array

javascript
Array.of(...item);
ParameterTypeDescription
itemObjectObject

Return Value

Type Array<Object> Returns an array

Examples

javascript
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

javascript
Array.toList(item);
ParameterTypeDescription
itemObjectObject, returns a clone if item is already an array

Return Value

Type Array<Object> Returns an array

Examples

javascript
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

javascript
Array.join(array, separator);
ParameterTypeDescription
arrayArrayArray to join
separatorStringSeparator

Return Value

Type String Returns the joined string

Examples

javascript
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); //null

length

Number of elements in the list

javascript
Array.length(list);
ParameterTypeDescription
listArrayArray to get length of

Return Value

Type Integer Returns the length of the array, returns 0 if the array is null

Examples

javascript
Array.length([1, 2, 3, 4]); //4
Array.length([1, null, 3, 4]); //4
Array.length(null); //0

get

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;

javascript
Array.get(list, key);
ParameterTypeDescription
listArray or MapCollection to get element from
keyInteger or StringElement index or key value

Return Value

Type Object Returns the element in the collection, returns null if the element doesn't exist

Examples

javascript
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); //null

set

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

javascript
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

javascript
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

javascript
Array.add(list, item);
ParameterTypeDescription
listArrayCollection to add element to
itemObjectElement to add

Return Value

Type Array Returns the collection after adding the element

Examples

javascript
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" }); //null

remove

Remove the element from the collection, if the collection is of Map type, item is the key

javascript
Array.remove(list, item);
ParameterTypeDescription
listArray or MapCollection to remove element from
itemObject or StringElement to remove, element's key

Return Value

Type Array or Map Returns the collection after removing the element

Examples

javascript
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

javascript
Array.removeAll(list, item);
ParameterTypeDescription
listArray or MapCollection to remove elements from
itemObject or String or ArrayElement to remove, element's key, array of elements

Return Value

Type Array or Map Returns the collection after removing the elements

Examples

javascript
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

javascript
Array.isEmpty(list);
ParameterTypeDescription
listArray or MapCollection to check length

Return Value

Type Boolean Whether the collection is empty

Examples

javascript
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(""); //true

isNotEmpty

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

javascript
Array.isNotEmpty(list);
ParameterTypeDescription
listArray or MapCollection to check length

Return Value

Type Boolean Whether the collection is not empty

Examples

javascript
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(""); //false

contains

Check if the list contains the item

javascript
Array.contains(list, item);
ParameterTypeDescription
listArray or MapCollection
itemObjectElement

Return Value

Type Boolean Whether the collection Array or Map contains the item

Examples

javascript
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"); //false

containsAny

Check if list1 contains any element from list2

Always returns false when list1 is null, always returns true when list2 is empty

javascript
Array.containsAny(list1, list2);
ParameterTypeDescription
list1ArrayCollection 1
list2ArrayCollection 2

Return Value

Type Boolean Whether list1 contains any element from list2

Examples

javascript
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); //false

containsAll

Check if list1 contains all elements from list2

Always returns false when list1 or list2 is empty

javascript
Array.containsAll(list1, list2);
ParameterTypeDescription
list1ArrayCollection 1
list2ArrayCollection 2

Return Value

Type Boolean Whether list1 contains all elements from list2

Examples

javascript
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); //false

map

Return a list of the key attributes in the list

javascript
Array.map(list, key);
ParameterTypeDescription
listArrayCollection 1
keyStringAttribute key

Return Value

Type Array List of each element's key attribute in the list

Examples

javascript
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

javascript
Array.props(list, props);
ParameterTypeDescription
listArrayCollection 1
propsArray<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

javascript
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

javascript
Array.transform(list, mapping);
ParameterTypeDescription
listArrayCollection 1
mappingMap<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

javascript
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

javascript
Array.concat(list1, list2);
ParameterTypeDescription
list1ArrayCollection 1
list2ArrayCollection 2

Return Value

Type Array Collection after concatenating list1 and list2

Examples

javascript
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

javascript
Array.sort(list, key);
ParameterTypeDescription
listArrayCollection
keyStringSorting attribute

Return Value

Type Array Sorted collection

Examples

javascript
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); //null

distinct

Return the elements in the list with duplicates removed

javascript
Array.distinct(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Array Collection after removing duplicates

Examples

javascript
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); //null

reverse

Return the list reversed

javascript
Array.reverse(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Array Reversed collection

Examples

javascript
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); //null

repeat

Generate an array with each item as element and length count

javascript
Array.repeat(count, element);
ParameterTypeDescription
countIntegerRepeat times
elementObjectElement to repeat

Return Value

Type Array Generated array

Examples

javascript
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

javascript
Array.sublist(list, fromIndex, toIndex);
ParameterTypeDescription
listArrayCollection
fromIndexIntegerStart index, starts from 0 if null
endIndexIntegerEnd 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

javascript
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); //null

filter

Filter items from the list where the value of the item's property key equals value

javascript
Array.filter(list, key, value);
ParameterTypeDescription
listArrayCollection
keyStringProperty key to compare
valueObjectProperty value to compare

Return Value

Type Array Array of items that meet the filtering condition

Examples

javascript
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

javascript
Array.shift(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Object First element

Examples

javascript
Array.shift([1, 2, 3]); //1
Array.shift([]); //null
Array.shift(null); //null

pop

Remove the last element from the list and return the removed element

javascript
Array.pop(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Object Last element

Examples

javascript
Array.pop([1, 2, 3]); //3
Array.pop([]); //null
Array.pop(null); //null

sum

Sum the elements in a list of numeric types

javascript
Array.sum(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Double Sum result

Examples

javascript
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 fails

avg

Calculate the average of elements in a list of numeric types

javascript
Array.avg(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Double Average result

Examples

javascript
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 fails

max

Calculate the maximum value of elements in a list of numeric types

javascript
Array.max(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Double Maximum value in the list

Examples

javascript
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 fails

min

Calculate the minimum value of elements in a list of numeric types

javascript
Array.min(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Double Minimum value in the list

Examples

javascript
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 fails

first

Return the first element in the list

javascript
Array.first(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Object First element in the list

Examples

javascript
Array.first([1, 2, 3]); //1
Array.first([]); //null
Array.first(null); //null

last

Return the last element in the list

javascript
Array.last(list);
ParameterTypeDescription
listArrayCollection

Return Value

Type Object Last element in the list

Examples

javascript
Array.last([1, 2, 3]); //3
Array.last([]); //null
Array.last(null); //null