Functions

countBy

countBy(Array<T>, (T) → Boolean): Number

Counts the matching elements using a matchingFunction(T)

Transform
1
2
3
4
%dw 2.0
output application/json
---
[1, 2, 3] countBy (($ mod 2) == 0)
Output
1
1

divideBy

divideBy(Array<T>, Number): Array<Array<T>>

Divides an Array of items into sub arrays. .Transform

1
2
3
output application/json
---
[1, 2, 3, 4, 5] divideBy 2
Output
1
2
3
4
5
6
7
8
9
10
[
   [
     1,
     2
   ],
   [
     4,
     5
   ]
 ]

every

every(Array<T>, (T) → Boolean): Boolean

Returns true if every element of the list matches the condition. It stops the iterations after the first negative evaluation. .Transform

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%dw 2.0
import * from dw::core::Arrays
var arr0: Array<Number> = []
output application/json
---
{
  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] every ($ == 1),
    [1,1,1] some ($ == 1),
    [1] every ($ == 1),
    [1] some ($ == 1),
  ],
  err: [
    [1,2,3] every ((log('should stop at 2 ==', $) mod 2) == 1),
    [1,2,3] some ($ == 100),
    [1,1,0] every ($ == 1),
    [0,1,1,0] every (log('should stop at 0 ==', $) == 1),
    [1] some ($ == 2),
    [1,2,3] every ($ == 1),
    arr0 every true,
    arr0 some true,
    arr0 some ($ is Number)
  ]
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "ok": [
    true,
    true,
    true,
    true,
    true,
    true,
    true,
    true
  ],
  "err": [
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false
  ]
}

some

some(Array<T>, (T) → Boolean): Boolean

Returns true if some element of the list matches the condition. It stops the iterations after the first match.

For an example, see every.

sumBy

sumBy(Array<T>, (T) → Number): Number

Adds the values returned by the right hand side function

Transform
1
2
3
4
5
%dw 2.0
output application/json
---
sumBy([ { a: 1 }, { a: 2 }, { a: 3 } ], (item) -> item.a)
// same as [ { a: 1 }, { a: 2 }, { a: 3 } ] sumBy $.a
Output
1
6