This module contains helper functions for working with arrays.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Arrays to the header of your
DataWeave script.
Functions
countBy
countBy(Array<T>, (T) → Boolean): Number
Counts the elements in a list (array) that match the results of a function.
Parameters
| Name | Description |
|---|---|
|
The input list that contains elements to match. |
|
A function to apply to the input list. |
Example
This counts the values in the list that are equal to the result of the
matchingFunction ((($ mod 2) == 0)).
Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{ "countBy" : [1, 2, 3, 4] countBy (($ mod 2) == 0) }
Output
1
{ "countBy": 2 }
divideBy
divideBy(Array<T>, Number): Array<Array<T>>
Breaks up a list (array) into sub-lists (sub-arrays) that contain the specified number of elements.
If there are fewer elements in the input array than the specified number, the function will fill the sub-array with those elements. If there are more elements, the function will fill as many sub-arrays needed with the extra elements.
Parameters
| Name | Description |
|---|---|
|
Items in the input array. |
|
The number of elements allowed per sub-array. |
Example
This example breaks up arrays into sub-arrays based on the specified amount.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
"divideBy" : [
{ "divideBy2" : [1, 2, 3, 4, 5] divideBy 2 },
{ "divideBy2" : [1, 2, 3, 4, 5, 6] divideBy 2 },
{ "divideBy3" : [1, 2, 3, 4, 5] divideBy 3 }
]
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"divideBy": [
{
"divideBy2": [
[ 1, 2 ],
[ 3, 4 ],
[ 5 ]
]
},
{
"divideBy2": [
[ 1, 2 ],
[ 3, 4 ],
[ 5, 6 ]
]
},
{
"divideBy3": [
[ 1, 2, 3 ],
[ 4, 5 ]
]
}
]
}
every
every(Array<T>, (T) → Boolean): Boolean
Returns true if every element in the list (array) matches the condition.
The function stops iterating after the first negative evaluation of a list.
Parameters
| Name | Description |
|---|---|
|
The input list. |
|
A condition (or expression) to apply to the input list. |
Example
This example applies a variety of expressions to the input lists.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%dw 2.0
import * from dw::core::Arrays
var arr0 = [] as Array<Number>
output application/json
---
{ "results" : [
"ok" : [
[1,1,1] every ($ == 1),
[1] every ($ == 1)
],
"err" : [
[1,2,3] every ((log('should stop at 2 ==', $) mod 2) == 1),
[1,1,0] every ($ == 1),
[0,1,1,0] every (log('should stop at 0 ==', $) == 1),
[1,2,3] every ($ == 1),
arr0 every true,
]
]
}
Output
1
2
3
4
5
6
7
8
9
10
{
"results": [
{
"ok": [ true, true ]
},
{
"err": [ false, false, false, false, false ]
}
]
}
some
some(Array<T>, (T) → Boolean): Boolean
Returns true if an element in the list (an array) matches the specified condition.
The function stops iterating after the first match to an element in the list.
Parameters
| Name | Description |
|---|---|
|
The input list. |
|
A condition (or expression) to apply to the input list. |
Example
This example applies a variety of expressions to the input lists.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{ "results" : [
"ok" : [
[1,2,3] some (($ mod 2) == 0),
[1,2,3] some (($ mod 2) == 1),
[1,2,3,4,5,6,7,8] some (log('should stop at 2 ==', $) == 2),
[1,2,3] some ($ == 1),
[1,1,1] some ($ == 1),
[1] some ($ == 1)
],
"err" : [
[1,2,3] some ($ == 100),
[1] some ($ == 2)
]
]
}
Output
1
2
3
4
5
6
7
8
9
10
{
"results": [
{
"ok": [ true, true, true, true, true, true ]
},
{
"err": [ false, false ]
}
]
}
sumBy
sumBy(Array<T>, (T) → Number): Number
Returns the sum of the values of the elements in a list (an array).
Parameters
| Name | Description |
|---|---|
|
The input list. |
|
A DataWeave selector that selects the values of the numbers in the input list. |
Example
This example calculates the sum of the values of elements some lists. Notice
that both of the sumBy function calls produce the same result.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
"sumBy" : [
[ { a: 1 }, { a: 2 }, { a: 3 } ] sumBy $.a,
sumBy([ { a: 1 }, { a: 2 }, { a: 3 } ], (item) -> item.a)
]
}
Output
1
{ "sumBy" : [ 6, 6 ] }