filter

filter<T>(@StreamCapable items: Array<T>, criteria: (item: T, index: Number) -> Boolean): Array<T>

配列を反復処理し、一致する値を返す式を適用します。

式は ​true​ または ​false​ を返す必要があります。配列の値またはインデックスに対して式が ​true​ を返した場合、その値は出力配列に取得されます。 配列の値またはインデックスに対して式が ​false​ を返した場合、その項目は出力から除外されます。一致する値がない場合、出力配列は空になります。

パラメーター

名前 説明

items

絞り込む配列。

criteria

item​ または ​index​ を選択するブール式。

次の例では、配列内で ​2​ より大きい値の配列を返します。

ソース

[9,2,3,4,5] filter (value, index) -> (value > 2)

出力

[9,3,4,5]

次の例では、年齢が 30 歳以上のすべてのユーザーの配列を返します。 このスクリプトでは、ラムダ式内の各要素のデータにアクセスします。

ソース

%dw 2.0
---
[{name: "Mariano", age: 37}, {name: "Shoki", age: 30}, {name: "Tomo", age: 25}, {name: "Ana", age: 29}]
          filter ((value, index) -> value.age >= 30)

出力

[
   {
     "name": "Mariano",
     "age": 37
   },
   {
     "name": "Shoki",
     "age": 30
   }
]

次の例では、​1​ より大きいインデックス (​$$​) で検出された、値が ​5​ 未満のすべての項目の配列を返します。これは、匿名関数で名前付きパラメーターを使用するのではなく、匿名パラメーターをセレクターとして使用しています。

ソース

%dw 2.0
output application/json
---
[9, 2, 3, 4, 5] filter (($$ > 1) and ($ < 5))

出力

[3,4]

この例では、​user​ キーと ​error​ キーを使用するオブジェクトを含む JSON 配列を読み取り、​filter​ 関数を使用して、​error​ キーの値が ​null​ のオブジェクトのみを返します。

ソース

%dw 2.0
output application/json

var users = [
   {
      "user": {
         "name": "123",
         "lastName": "Smith"
      },
      "error": "That name doesn't exists"
   },
   {
      "user": {
         "name": "John",
         "lastName": "Johnson"
      },
      "error": null
   }
]
---
users filter ((item, index) -> item.error == null)

出力

[
  {
    "user": {
      "name": "John",
      "lastName": "Johnson"
    },
    "error": null
  }
]

次の例では、JSON 配列を読み取り、​filter​ 関数を使用して有効な電話番号を抽出します。

入力

{
  "Id": "1184001100000000517",
  "marketCode": "US",
  "languageCode": "en-US",
  "profile": {
  "base": {
     "username": "TheMule",
     "activeInd": "R",
     "phone": [
       {
          "activeInd": "Y",
          "type": "mobile",
          "primaryInd": "Y",
          "number": "230678123"
       },
       {
         "activeInd": "N",
         "type": "mobile",
         "primaryInd": "N",
         "number": ""
       },
       {
          "activeInd": "Y",
          "type": "mobile",
          "primaryInd": "Y",
          "number": "154896523"
       }

      ]
    }
  }
 }

ソース

%dw 2.0
output application/json
---
{
    id: payload.Id,
    markCode: payload.marketCode,
    languageCode: payload.languageCode,
    username: payload.profile.base.username,
    phoneNumber: (payload.profile.base.phone filter ($.activeInd == "Y" and $.primaryInd== "Y")).number default []
}

出力

{
  "id": "1184001100000000517",
  "markCode": "US",
  "languageCode": "en-US",
  "username": "TheMule",
  "phoneNumber": [
    "230678123"
    "154896523"
  ]
}

filter(@StreamCapable text: String, criteria: (character: String, index: Number) -> Boolean): String

文字列を反復処理し、一致する値を返す式を適用します。

式は ​true​ または ​false​ を返す必要があります。配列の文字またはインデックスに対して式が ​true​ を返した場合、その文字は出力文字列に取得されます。 配列の文字またはインデックスに対して式が ​false​ を返した場合、その文字は出力から除外されます。一致する値がない場合、出力文字列は空になります。

パラメーター

名前 説明

text (テキスト)

絞り込むテキスト。

criteria

使用する条件。

次の例では、​filter​ を使用して偶数番目にあるすべての文字を削除する方法を示しています。

ソース

%dw 2.0
output application/json
---
"hello world" filter ($$ mod 2) == 0

出力

"hlowrd"

filter(@StreamCapable value: Null, criteria: (item: Nothing, index: Nothing) -> Any): Null

filter​ を ​null​ 値に使用できるようにするヘルパー関数。