Filtering Array Elements to Map Based on a Condition
When mapping arrays of objects, you can filter the objects that gets mapped by specifying a filter in the mapper.
You specify this filter as the third argument in the
array.forEach()
function, the first argument being the scope of the element being mapped and the second argument being the scoping variable.
Before you begin To specify the filter as the third argument, you must mandatorily specify both the first two arguments in
array.forEach()
- the scope as well as the scoping variable.
Here's an example that contains a filter as the third argument:
The above example indicates the following:
- objArray1 is being mapped to objArray in Upstream Output
- when iterating through objArray in the Upstream Output, only the array elements in objArray whose child element, user, is "Jane" get mapped. If user is not equal to "Jane" the iteration for that object is skipped and objArray1 does not acquire that object.
- $loop here specifies the scope of the current loop that is being iterated, in this case objArray, whose scope is objArray1 in Upstream Output.