ArrayGroupBy
Returns a struct of keys returned from the predicate function and values of arrays of matching rows.
values = [ 1, 2, 3, 4 ];
values.groupBy( ( value ) => value % 2 ? "odd" : "even" );
// { odd: [ 1, 3 ], even: [ 2, 4 ] }
Method Signature
ArrayGroupBy(array=[array], callback=[function])Arguments
Argument
Type
Required
Description
Default
array
array
true
The array to group.
callback
function
true
The function to invoke for each item. The function will be passed 3 arguments: the value, the index, the array. You can alternatively pass a Java Predicate which will only receive the 1st arg.
Examples
Group array elements by a computed key
Returns a struct where each key is a value returned by the callback and each value is an array of matching elements.
numbers = [ 1, 2, 3, 4, 5, 6 ];
groups = numbers.groupBy( ( n ) => n % 2 == 0 ? "even" : "odd" );
writeOutput( groups.keyList() );
Result: odd,even
Group objects by a property
Result: 2
Access a specific group
Result: [2, 4]
Related
Last updated
Was this helpful?
