---
schema_version: "1"
id: 11f1929d493b35ee77d057fbf6c30ce2787853871e1dfe65cb87a83b83d1afae
title: Conditionally Reduce a List through a Function
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-cookbook-conditional-list-reduction-via-function.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-conditional-list-reduction-via-function
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 287d4abf2502a5f2dc8aaf061104d31f4039b5bb3013e2c280b43122d0b4801f
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Conditionally Reduce a List through a Function

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

This DataWeave example flattens the input to something simpler. For each element in the input, it
conditionally includes a `contentTypes` field only in case it has any values. It then rearranges
this content into a more readable shape.

The examples use these functions:

- `map` to go through every element in the input array.

- `reduce` to aggregate the multiple elements in the `tags` array into one field.

- `splitBy` to parse the values in the "tags" array, so as to only select the first part of each
  string (through `[0]`) and discard the second.

- `++` to concatenate these elements into one single string, with commas as separators.

- `if` include the last field only if the `contentTypes` array is bigger than 0.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
fun reduceMapFor(data) = data reduce(($$ splitBy ":")[0] ++ "," ++ ($ splitBy ":")[0])
---
payload.results map
    {
        email: $.profile.email,
        name: $.profile.firstName,
        tags: reduceMapFor($.data.interests.tags[0]),
        (contenttypes: reduceMapFor($.data.interests.contenttypes[0])) if (sizeOf($.data.interests.contenttypes[0]) > 0)
    }
```

Input JSON Payload:

```json
{
  "results": [
    {
      "profile": {
        "firstName": "john",
        "lastName": "doe",
        "email": "johndoe@demo.com"
      },
      "data": {
        "interests": [
          {
            "language": "English",
            "tags": ["digital-strategy:Digital Strategy", "innovation:Innovation"],
            "contenttypes": []
          }
        ]
      }
    },
    {
      "profile": {
        "firstName": "jane",
        "lastName": "doe",
        "email": "janedoe@demo.com"
      },
      "data": {
        "interests": [
          {
            "language": "English",
            "tags": ["tax-reform:Tax Reform", "retail-health:Retail Health"],
            "contenttypes": ["News", "Analysis", "Case studies", "Press releases"]
          }
        ]
      }
    }
  ],
  "objectsCount": 2,
  "totalCount": 2,
  "statusCode": 200,
  "errorCode": 0,
  "statusReason": "OK"
}
```

Output JSON:

```json
[
  {
    "email": "johndoe@demo.com",
    "name": "john",
    "tags": "digital-strategy,innovation"
  },
  {
    "email": "janedoe@demo.com",
    "name": "jane",
    "tags": "tax-reform,retail-health",
    "contenttypes": "News,Analysis,Case studies,Press releases"
  }
]
```

## Related Examples

- [Exclude Fields from the Output](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-exclude-field)

- [Output a Field When Present](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-output-a-field-when-present)

- [Use Constant Directives](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-use-constant-directives)

- [Define a Function that Flattens Data in a List](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-define-function-to-flatten-list)

- [Pass Functions as Arguments](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-pass-functions-as-arguments)

## See Also

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.6/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook)
