Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

groupBy

groupBy(Array<T>, (item: T, index: Number) → R): { (R): Array<T> }

Partitions an Array into a Object that contains Arrays, according to the discriminator function (a lambda) that you define.

The lambda is invoked with three parameters: value, key, and index. If these parameters are not named, the value is defined by default as $, the key $$ and the index $$$.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
"language": payload.langs groupBy $.language
Input

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "langs": [
    {
      "name": "Foo",
      "language": "Java"
    },
    {
      "name": "Bar",
      "language": "Scala"
    },
    {
      "name": "FooBar",
      "language": "Java"
    }
  ]
}
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
{
  "language": {
    "Scala": [
        {"name":"Bar", "language":"Scala"}
      ],
    "Java": [
        {"name":"Foo", "language":"Java"},
        {"name":"FooBar", "language":"Java"}
      ]
  }
}
groupBy({ (K)?: V }, (value: V, key: K) → R): { (R): Array<T> }

Partitions an Object into a Object that contains Arrays, according to the discriminator lambda you define.

The lambda is invoked with two parameters: value and the key.

groupBy(Null, (Nothing, Nothing) → Any): Null

Helper function that allows groupBy to work with null values.