---
schema_version: "1"
id: 9ae071190a1fb3096d1c1d2aa94a9af17d992ef464874e3c1f7c3a1cf9de3039
title: 関数を使用したリストの条件付き削減
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.7/dataweave-cookbook-conditional-list-reduction-via-function.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-conditional-list-reduction-via-function
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 1c6dcc8b38875667db83bb685a12786670a5372855aaf7f9bc13f12185086c42
description: MuleSoft Documentation Site
release: "2.7"
latest: false
---
# 関数を使用したリストの条件付き削減

この DataWeave の例では、入力をフラット化してシンプルにします。入力の各要素で、値がある場合にのみ ​`contentTypes`​ 項目を含めます。次に、このコンテンツをよりわかりやすい形式に再編成します。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

この例では、次の関数を使用します。

- 入力配列の各要素を調べる ​`map`​。
- `tags`​ 配列の複数の要素を 1 つの項目に集約する ​`reduce`​。
- (​`[0]`​ で) 各文字列の最初の部分のみを選択して 2 番目の部分を破棄するために「tags」配列の値を解析する ​`splitBy`​。
- 区切り文字としてカンマを使用して、これらの要素を 1 つの文字列に連結する ​`++`​。
- `contentTypes`​ 配列が 0 より大きい場合にのみ最後の項目を含める ​`if`​。

DataWeave スクリプト:

```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)
    }
```

入力 JSON ペイロード:

```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"
}
```

出力 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"
  }
]
```

## [](#関連する例)関連する例

- [出力からの項目の除外](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-exclude-field)
- [存在する項目の出力](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-output-a-field-when-present)
- [定数ディレクティブの使用](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-use-constant-directives)
- [リスト内のデータをフラット化する関数の定義](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-define-function-to-flatten-list)
- [関数を引数として渡す](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-pass-functions-as-arguments)

## [](#関連情報)関連情報

- [DataWeave セレクター](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-selectors)
- [DataWeave の解説書](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook)