---
schema_version: "1"
id: 8adcc60c4e8ad8c9dc3d6490bddefb1369a7cb09c36a2cdcc681d6c7fdc0383c
title: フラット化された配列要素
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.7/dataweave-cookbook-flatten-arrays.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-flatten-arrays
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 3f587315794752b4a195c3be8be8f0a7146511d631373d450e0b46f10312daab
description: MuleSoft Documentation Site
release: "2.7"
latest: false
---
# フラット化された配列要素

DataWeave では、DataWeave オブジェクト、配列、またはサブ配列において、配列のサブ配列やキー/値ペアのコレクションをフラット化できます。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

## [](#キー値ペアを持つサブ配列のオブジェクト配列へのフラット化)キー/値ペアを持つサブ配列のオブジェクト配列へのフラット化

次の例では、​`flatten`​ を使用して、変数 ​`arrayOne`​ で定義される入力配列のキー-値ペアをフラット化しています。配列内のすべてのキー/値ペアは個別の DataWeave オブジェクトになります。

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

- `flatten`​ は、サブ配列の要素を親配列に移動して、サブ配列を除去し、すべてのキー/値ペアを親配列内のオブジェクトのリストに変換します。

DataWeave スクリプト:

```dataweave
%dw 2.0
var arrayOne = [
                 [
                    "keyOne" : 1,
                    "keyTwo" : 2
                 ],
                 [
                    "keyThree" : 3,
                    "keyFour" : 4,
                    "keyFive" : 5
                 ],
                 "keySix" : 6
               ]
output application/json
---
flatten(arrayOne)
```

出力 JSON:

```json
[
  {
    "keyOne": 1
  },
  {
    "keyTwo": 2
  },
  {
    "keyThree": 3
  },
  {
    "keyFour": 4
  },
  {
    "keyFive": 5
  },
  {
    "keySix": 6
  }
]
```

## [](#組み合わせた配列のフラット化)組み合わせた配列のフラット化

上の DataWeave の例と同じように、次の例は、2 つの配列の要素を組み合わせてフラット化します。この 2 番目の例では、セレクター ​`fruit`​ を使用してキー ​`fruit`​ の値のみを選択し、配列内の他の値は除外します。

この例では、以下を使用します。

- `++`​ は、2 つの配列 (​`arrayOne`​ と ​`arrayTwo`​) のオブジェクトを 1 つの配列に組み合わせます。
- `flatten`​ は、組み合わせた配列を、キー/値ペアのリストとしてフラット化します。

DataWeave スクリプト:

```dataweave
%dw 2.0
var arrayOne = [
                  [
                     "fruit" : "orange",
                     "fruit" : "apple"
                  ],
                  [
                     "fruit" : "grape",
                     "notfruit" : "something else"
                  ]
               ]
var arrayTwo = [
                  [
                  	{  "fruit" : "kiwi" }
                  ],
                  "fruit" : "strawberry",
                  "fruit" : "plum",
                  { "fruit" : "banana" },
                  "notfruit" : "something else"
               ]
output application/json
---
flatten(arrayOne ++ arrayTwo)
```

出力 JSON:

```json
[
  { "fruit": "orange" },
  { "fruit": "apple" },
  { "fruit": "grape" },
  { "notfruit": "something else" },
  { "fruit": "kiwi" },
  { "fruit": "strawberry" },
  { "fruit": "plum" },
  { "fruit": "banana" },
  { "notfruit": "something else" }
]
```

[上の例](#ex02)​と次の例の唯一の違いは、​`.fruit`​ セレクターを本文の式に追加して、​`fruit`​ 値をすべて選択し、​`notfruit`​ キーの値を出力配列から除外しているという点です。

DataWeave スクリプト:

```dataweave
%dw 2.0
var arrayOne = [
                  [
                     "fruit" : "orange",
                     "fruit" : "apple"
                  ],
                  [
                     "fruit" : "grape",
                     "notfruit" : "something else"
                  ]
               ]
var arrayTwo = [
                  [
                  	{  "fruit" : "kiwi" }
                  ],
                  "fruit" : "strawberry",
                  "fruit" : "plum",
                  { "fruit" : "banana" },
                  "notfruit" : "something else"
               ]
output application/json
---
flatten(arrayOne ++ arrayTwo).fruit
```

出力 JSON:

```json
[
  "orange",
  "apple",
  "grape",
  "kiwi",
  "strawberry",
  "plum",
  "banana"
]
```

次の例では、本文の式で ​`.fruit`​ ではなく ​`..*fruit`​ をセレクターとして使用し、ネストされているすべての ​`fruit`​ 値を、フラット化した配列に返しています。また、2 つではなく 3 つの配列を組み合わせてフラット化しています。

DataWeave スクリプト:

```dataweave
%dw 2.0
var arrayOne = [
                  [
                     "fruit" : "orange",
                     "fruit" : "apple"
                  ],
                  [
                     "fruit" : "grape",
                     "notfruit" : "something else"
                  ]
               ]
var arrayTwo = [
                  [
                  	{  "fruit" : "kiwi" }
                  ],
                  "fruit" : "strawberry",
                  "fruit" : "plum",
                  { "fruit" : "banana" },
                  "notfruit" : "something else"
               ]
var arrayThree = [
                    { parentOne :
                        [
                     	     { child :
                     	       [
                     	         { grandchild :
                                   {
                                      "fruit" : "watermelon",
                                      "notfruit" : "something else"
                                   }
                                },
                                {
                                   fruit : "cantaloupe",
                                   "notfruit" : "something else"
                                }
                             ]
                           },
                           {
                              fruit : "honeydew",
                              "notfruit" : "something else"
                           }
                        ]
                    },
                    { parentTwo:
                     	  [
                     	  	 fruit : "cherry",
                     		   "notfruit" : "something else"
                     	  ]
                    }
                 ]
output application/json
---
flatten(arrayOne ++ arrayTwo ++ arrayThree)..*fruit
```

出力 JSON:

```json
[
  "orange",
  "apple",
  "grape",
  "kiwi",
  "strawberry",
  "plum",
  "banana",
  "watermelon",
  "cantaloupe",
  "honeydew",
  "cherry"
]
```

## [](#親配列へのサブ配列のフラット化)親配列へのサブ配列のフラット化

次の例では、​`flatten`​ 関数をさまざまなデータ型 (数値、DataWeave オブジェクト、サブ配列、文字列、​`null`​ 値、キー/値ペア) に対して使用しています。サブ配列の要素は親配列の要素になり、サブ配列は除去されます。

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

- `flatten`​ は、サブ配列をフラット化して親配列に含めます。

DataWeave スクリプト:

```dataweave
%dw 2.0
var myArray = [
                 1,
                 [2,3],
                 { a : "b"},
                 "my string",
                 [ [4,5], { c : "d"}, 6 ],
                 null,
                 "e" : "f"
              ]
output application/json
---
flatten(myArray)
```

出力 JSON:

```json
[
  1,
  2,
  3,
  {
    "a": "b"
  },
  "my string",
  [
    4,
    5
  ],
  {
    "c": "d"
  },
  6,
  null,
  {
    "e": "f"
  }
]
```

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

- [flatten](https://docs.mulesoft.com/jp/dataweave/2.7/dw-core-functions-flatten)
- [リスト内のデータをフラット化する関数の定義](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-cookbook-define-function-to-flatten-list)
- [セレクター](https://docs.mulesoft.com/jp/dataweave/2.7/dataweave-selectors)