Filtering Array Elements to Map Based on a Condition

When mapping arrays of objects, you can filter the objects that are 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.

To specify the filter as the third argument, you must 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 Available data
  • When iterating through objArray in the Available data, 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 Available data.