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

array

The input array that contains elements to match.

matchingFunction

A function to apply to elements in the input array.

Example

This counts the values in the array 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.

When there are fewer elements in the input array than the specified number, the function fills the sub-array with those elements. When there are more elements, the function fills as many sub-arrays needed with the extra elements.

Parameters
Name Description

items

Items in the input array.

amount

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 ]
      ]
    }
 ]
}

drop

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

Drops first n elements. It returns the original array when n ⇐ 0 and an empty array when n > sizeOf(array).

Since Version: 2.2.0 ===== Parameters

Name Description

array

The left Array of elements.

n

The number of elements to take.

Example
Source
1
2
3
4
5
6
 %dw 2.0
 import * from dw::core::Arrays
 var users = ["Mariano", "Leandro", "Julian"]
 output application/json
 ---
 drop(users, 2)
Output
1
2
3
[
  "Julian"
]

dropWhile

dropWhile(Array<T>, (item: T) → Boolean): Array<T>

Drops elements from the array while the condition is met.

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements.

condition

The condition (or expression) used to match an element in the array

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,3,4,5]
---
arr dropWhile $ <= 2
Output
1
2
3
4
5
[
  3,
  4,
  5
]

every

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

Returns true if every element in the array matches the condition.

The function stops iterating after the first negative evaluation of an element in the array.

Parameters
Name Description

list

The input array.

condition

A condition (or expression) to apply to elements in the input array.

Example

This example applies a variety of expressions to the input arrays. The $ references values of the elements.

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 ]
     }
   ]
 }

indexOf

indexOf(Array<T>, T): Number

Returns the index of the first occurrence of an element within the array.

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements.

toFind

The element to find.

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var users = ["Mariano", "Leandro", "Julian"]
---
indexOf(users, "Julian")
Output
1
2

indexWhere

indexWhere(Array<T>, (item: T) → Boolean): Number

Returns the index of the first occurrence of an element that matches a condition within the array.

Since Version: 2.2.0

Parameters
Name Description

array

The Array of elements.

condition

The condition (or expression) used to match an element in the array

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var users = ["Mariano", "Leandro", "Julian"]
---
users indexWhere (item) -> item startsWith "Jul"
Output
1
2

join

join(Array<L>, Array<R>, (leftValue: L) → String, (rightValue: R) → String): Array<Pair<L, R>>

Joins two array of objects by a given ID criteria. It will return an Array of object containing only the ones that the ID of the left can be matched with an ID of the right.

Since Version: 2.2.0 ===== Parameters

Name Description

left

The left Array of objects.

right

The right Array of objects.

leftCriteria

The criteria used to extract the ID for the left collection.

rightCriteria

The criteria used to extract the ID for the right collection.

Example

This example shows how join behaves

Source
1
2
3
4
5
6
7
 %dw 2.0
 import * from dw::core::Arrays
 var users = [{id: "1", name:"Mariano"},{id: "2", name:"Leandro"},{id: "3", name:"Julian"},{id: "5", name:"Julian"}]
 var products = [{ownerId: "1", name:"DataWeave"},{ownerId: "1", name:"BAT"}, {ownerId: "3", name:"DataSense"}, {ownerId: "4", name:"SmartConnectors"}]
 output application/json
 ---
 join(users, products, (user) -> user.id, (product) -> product.ownerId)
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
25
26
27
28
29
30
31
32
[
  {
    "l": {
      "id": "1",
      "name": "Mariano"
    },
    "r": {
      "ownerId": "1",
      "name": "DataWeave"
    }
  },
  {
    "l": {
      "id": "1",
      "name": "Mariano"
    },
    "r": {
      "ownerId": "1",
      "name": "BAT"
    }
  },
  {
    "l": {
      "id": "3",
      "name": "Julian"
    },
    "r": {
      "ownerId": "3",
      "name": "DataSense"
    }
  }
]

leftJoin

leftJoin(Array<L>, Array<R>, (leftValue: L) → String, (rightValue: R) → String): Array<{ l: L, r?: R }>

Joins two array of objects by a given ID criteria. It will return an Array all the left items merged by ID with the right items in the case where it exists any.

Since Version: 2.2.0 ===== Parameters

Name Description

left

The left Array of objects.

right

The right Array of objects.

leftCriteria

The criteria used to extract the ID for the left collection.

rightCriteria

The criteria used to extract the ID for the right collection.

Example

This example shows how join behaves

Source
1
2
3
4
5
6
7
 %dw 2.0
 import * from dw::core::Arrays
 var users = [{id: "1", name:"Mariano"},{id: "2", name:"Leandro"},{id: "3", name:"Julian"},{id: "5", name:"Julian"}]
 var products = [{ownerId: "1", name:"DataWeave"},{ownerId: "1", name:"BAT"}, {ownerId: "3", name:"DataSense"}, {ownerId: "4", name:"SmartConnectors"}]
 output application/json
 ---
 leftJoin(users, products, (user) -> user.id, (product) -> product.ownerId)
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[
  {
    "l": {
      "id": "1",
      "name": "Mariano"
    },
    "r": {
      "ownerId": "1",
      "name": "DataWeave"
    }
  },
  {
    "l": {
      "id": "1",
      "name": "Mariano"
    },
    "r": {
      "ownerId": "1",
      "name": "BAT"
    }
  },
  {
    "l": {
      "id": "2",
      "name": "Leandro"
    }
  },
  {
    "l": {
      "id": "3",
      "name": "Julian"
    },
    "r": {
      "ownerId": "3",
      "name": "DataSense"
    }
  },
  {
    "l": {
      "id": "5",
      "name": "Julian"
    }
  }
]

outerJoin

outerJoin(Array<L>, Array<R>, (leftValue: L) → String, (rightValue: R) → String): Array<{ l?: L, r?: R }>

Joins two array of objects by a given ID criteria. It will return an Array with all the left items merged by ID with the left items in the case where it exists any, and the right items that are not present in the left

Since Version: 2.2.0 ===== Parameters

Name Description

left

The left Array of objects.

right

The right Array of objects.

leftCriteria

The criteria used to extract the ID for the left collection.

rightCriteria

The criteria used to extract the ID for the right collection.

Example

This example shows how join behaves

Source
1
2
3
4
5
6
7
 %dw 2.0
 import * from dw::core::Arrays
 var users = [{id: "1", name:"Mariano"},{id: "2", name:"Leandro"},{id: "3", name:"Julian"},{id: "5", name:"Julian"}]
 var products = [{ownerId: "1", name:"DataWeave"},{ownerId: "1", name:"BAT"}, {ownerId: "3", name:"DataSense"}, {ownerId: "4", name:"SmartConnectors"}]
 output application/json
 ---
 outerJoin(users, products, (user) -> user.id, (product) -> product.ownerId)
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[
  {
    "l": {
      "id": "1",
      "name": "Mariano"
    },
    "r": {
      "ownerId": "1",
      "name": "DataWeave"
    }
  },
  {
    "l": {
      "id": "1",
      "name": "Mariano"
    },
    "r": {
      "ownerId": "1",
      "name": "BAT"
    }
  },
  {
    "l": {
      "id": "2",
      "name": "Leandro"
    }
  },
  {
    "l": {
      "id": "3",
      "name": "Julian"
    },
    "r": {
      "ownerId": "3",
      "name": "DataSense"
    }
  },
  {
    "l": {
      "id": "5",
      "name": "Julian"
    }
  },
  {
    "r": {
      "ownerId": "4",
      "name": "SmartConnectors"
    }
  }
]

partition

partition(Array<T>, (item: T) → Boolean): { success: Array<T>, failure: Array<T> }

Separates the array into the elements that satisfy the condition and those which don’t.

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements to split.

condition

The condition (or expression) used to match an element in the array

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,3,4,5]
---
arr partition (item) -> isEven(item)
Output
1
2
3
4
5
6
7
8
9
10
11
12
{
  "success": [
    0,
    2,
    4
  ],
  "failure": [
    1,
    3,
    5
  ]
}

slice

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

Selects the interval of elements that satisfy the condition: from ⇐ indexOf(array) < until

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements.

from

The lowest index to include from the array.

until

The lowest index to exclude from the array.

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,3,4,5]
---
slice(arr, 1, 4)
Output
1
2
3
4
5
[
  1,
  2,
  3
]

some

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

Returns true if an element in the array matches the specified condition.

The function stops iterating after the first match to an element in the array.

Parameters
Name Description

list

The input array.

condition

A condition (or expression) used to match elements in the array.

Example

This example applies a variety of expressions to elements in input arrays. The $ references values of the elements.

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 ]
     }
   ]
 }

splitAt

splitAt(Array<T>, Number): Pair<Array<T>, Array<T>>

Splits an array into two at a given position.

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements.

n

The index to split at.

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var users = ["Mariano", "Leandro", "Julian"]
---
users splitAt 1
Output
1
2
3
4
5
6
7
8
9
{
  "l": [
    "Mariano"
  ],
  "r": [
    "Leandro",
    "Julian"
  ]
}

splitWhere

splitWhere(Array<T>, (item: T) → Boolean): Pair<Array<T>, Array<T>>

Splits an array into two at the first position where the condition is met.

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements to split.

condition

The condition (or expression) used to match an element in the array

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var users = ["Mariano", "Leandro", "Julian", "Tomo"]
---
users splitWhere (item) -> item startsWith "Jul"
Output
1
2
3
4
5
6
7
8
9
10
{
  "l": [
    "Mariano",
    "Leandro"
  ],
  "r": [
    "Julian",
    "Tomo"
  ]
}

sumBy

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

Returns the sum of the values of the elements in an array.

Parameters
Name Description

array

The input array.

numberSelector

A DataWeave selector that selects the values of the numbers in the input array.

Example

This example calculates the sum of the values of elements some arrays. 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 ] }

take

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

Selects first n elements. It returns an empty array when n ⇐ 0 and the original array when n > sizeOf(array).

Since Version: 2.2.0 ===== Parameters

Name Description

array

The left Array of elements.

n

The number of elements to take.

Example
Source
1
2
3
4
5
6
 %dw 2.0
 import * from dw::core::Arrays
 var users = ["Mariano", "Leandro", "Julian"]
 output application/json
 ---
 take(users, 2)
Output
1
2
3
4
[
  "Mariano",
  "Leandro"
]

takeWhile

takeWhile(Array<T>, (item: T) → Boolean): Array<T>

Takes elements for the array while the condition is met.

Since Version: 2.2.0 ===== Parameters

Name Description

array

The Array of elements.

condition

The condition (or expression) used to match an element in the array

Example
Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,3,4,5,1]
---
arr takeWhile $ <= 2
Output
1
2
3
4
5
[
  0,
  1,
  2
]