Contact Us 1-800-596-4880

filterArrayLeafs

filterArrayLeafs(value: Any, criteria: (value: Any, path: Path) -> Boolean): Any

Applies a filtering expression to leaf or Path values of an array.

The leaf values in the array must be SimpleType or Null values. See Core Types for descriptions of the types.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

value

An input value of Any type.

criteria

Boolean expression to apply to SimpleType or Null leaf values of all arrays in the input value. If the result is true, the array retains the leaf value. If not, the function removes the leaf value from the output.

Example

This example shows how filterArrayLeafs behaves with different inputs.

Source

%dw 2.0
import * from dw::util::Tree
var myArray = [1, {name: ["", true], test: 213}, "123", null]
output application/json
---
{
   a: myArray filterArrayLeafs ((value, path) ->
        !(value is Null or value is String)),
   b:  myArray filterArrayLeafs ((value, path) ->
        (value is Null or value == 1)),
   c: { a : [1,2] } filterArrayLeafs ((value, path) ->
        (value is Null or value == 1)),
   d: myArray filterArrayLeafs ((value, path) ->
        !isArrayType(path))
}

Output

{
  "a": [
    1,
    {
      "name": [
        true
      ],
      "test": 213
    }
  ],
  "b": [
    1,
    {
      "name": [

      ],
      "test": 213
    },
    null
  ],
  "c": {
    "a": [
     1
    ]
  },
  "d": [
    {
      "name": [

      ],
      "test": 213
    }
  ]
}