Contact Free trial Login

countBy

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

Counts the elements in an 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

 %dw 2.0
 import * from dw::core::Arrays
 output application/json
 ---
{ "countBy" : [1, 2, 3, 4] countBy (($ mod 2) == 0) }

Output

{ "countBy": 2 }