ARM テンプレート用の配列関数

この記事では、配列を操作するための テンプレート関数について説明します。

文字列値の配列をある値で区切られた状態にするには、「 split」を参照してください。

ヒント

Bicep は ARM テンプレートと同じ機能を提供し、構文の方が使いやすいため、推奨されます。 詳細については、 array 関数に関するページを参照してください。

アレイ

array(convertToArray)

値を配列に変換します。

Bicep で、 array 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
convertToArray はい 整数、文字列、配列、オブジェクト 配列に変換する値。

戻り値

配列。

例

次の例は、さまざまな型で array 関数を使用する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "intToConvert": {
      "type": "int",
      "defaultValue": 1
    },
    "stringToConvert": {
      "type": "string",
      "defaultValue": "efgh"
    },
    "objectToConvert": {
      "type": "object",
      "defaultValue": {
        "a": "b",
        "c": "d"
      }
    }
  },
  "resources": [
  ],
  "outputs": {
    "intOutput": {
      "type": "array",
      "value": "[array(parameters('intToConvert'))]"
    },
    "stringOutput": {
      "type": "array",
      "value": "[array(parameters('stringToConvert'))]"
    },
    "objectOutput": {
      "type": "array",
      "value": "[array(parameters('objectToConvert'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
intOutput 配列 [1]
stringOutput 配列 ["efgh"]
objectOutput 配列 [{"a": "b", "c": "d"}]

concat

concat(arg1, arg2, arg3, ...)

複数の配列を結合し、連結された配列を返します。または複数の文字列値を結合し、連結文字列を返します。

Bicep で、 concat 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列または文字列 連結する最初の配列または文字列。
その他の引数 いいえ 配列または文字列 順次連結するその他の配列または文字列。

この関数は任意の数の引数を受け取ることができ、パラメーターの文字列または配列を受け取ることができます。 ただし、パラメーターに配列と文字列の両方を指定することはできません。 配列は、他の配列とのみ連結されます。

戻り値

連結された値の文字列または配列。

例

次の例は、2 つの配列を結合する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstArray": {
      "type": "array",
      "defaultValue": [
        "1-1",
        "1-2",
        "1-3"
      ]
    },
    "secondArray": {
      "type": "array",
      "defaultValue": [
        "2-1",
        "2-2",
        "2-3"
      ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "return": {
      "type": "array",
      "value": "[concat(parameters('firstArray'), parameters('secondArray'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
戻り値 配列 ["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]

次の例は、2 つの文字列値を結合し、連結された文字列を返す方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "prefix": {
      "type": "string",
      "defaultValue": "prefix"
    }
  },
  "resources": [],
  "outputs": {
    "concatOutput": {
      "type": "string",
      "value": "[concat(parameters('prefix'), '-', uniqueString(resourceGroup().id))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
concatOutput 糸 prefix-5yj4yjf5mbg72

含む

contains(container, itemToFind)

配列に値が含まれているか、オブジェクトにキーが含まれているか、文字列に部分文字列が含まれているかどうかを確認します。 文字列比較では大文字・小文字を区別します。 ただし、オブジェクトにキーが含まれているかどうかをテストする場合、比較で大文字・小文字を区別しません。

Bicep で、 contains 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
コンテナ はい 配列、オブジェクト、文字列 検索対象の値を含む値。
itemToFind はい 文字列または整数 検索対象の値。

戻り値

関数がアイテムを見つけた場合は真です。そうでなければ、Falseです。

例

次の例では、contains をさまざまな型で使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stringToTest": {
      "type": "string",
      "defaultValue": "OneTwoThree"
    },
    "objectToTest": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c"
      }
    },
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "stringTrue": {
      "type": "bool",
      "value": "[contains(parameters('stringToTest'), 'e')]"
    },
    "stringFalse": {
      "type": "bool",
      "value": "[contains(parameters('stringToTest'), 'z')]"
    },
    "objectTrue": {
      "type": "bool",
      "value": "[contains(parameters('objectToTest'), 'one')]"
    },
    "objectFalse": {
      "type": "bool",
      "value": "[contains(parameters('objectToTest'), 'a')]"
    },
    "arrayTrue": {
      "type": "bool",
      "value": "[contains(parameters('arrayToTest'), 'three')]"
    },
    "arrayFalse": {
      "type": "bool",
      "value": "[contains(parameters('arrayToTest'), 'four')]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
stringTrue ブール 正しい
stringFalse ブール いいえ
objectTrue ブール 正しい
objectFalse ブール いいえ
arrayTrue ブール 正しい
arrayFalse ブール いいえ

createArray

createArray(arg1, arg2, arg3, ...)

パラメーターから配列を作成します。

Bicep では、createArray 関数はサポートされていません。 配列を構築するには、Bicep の array データ型を参照してください。

パラメーター

パラメーター 必須 タイプ 説明
引数 いいえ 文字列、整数、配列、オブジェクト 配列内の値。

戻り値

配列。 パラメータを何も提供しない場合は、空の配列を返します。

例

次の例では、createArray をさまざまな型で使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "objectToTest": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c"
      }
    },
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "stringArray": {
      "type": "array",
      "value": "[createArray('a', 'b', 'c')]"
    },
    "intArray": {
      "type": "array",
      "value": "[createArray(1, 2, 3)]"
    },
    "objectArray": {
      "type": "array",
      "value": "[createArray(parameters('objectToTest'))]"
    },
    "arrayArray": {
      "type": "array",
      "value": "[createArray(parameters('arrayToTest'))]"
    },
    "emptyArray": {
      "type": "array",
      "value": "[createArray()]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
stringArray 配列 [「a」「b」「c」]
intArray 配列 [1, 2, 3]
objectArray 配列 [{"one": "a", "two": "b", "three": "c"}]
arrayArray 配列 [["一"、"二"、"三"]]
emptyArray 配列 ).

distinct

distinct(arrayToModify)

入力配列からすべての重複値が削除された新しい配列を返します。 この関数は最初に出現した順序を保持します。

Bicep で、 distinct 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arrayToModify(配置配置) はい アレイ 重複を除去するための配列です。

戻り値

入力配列から一意な要素のみを含む配列です。 元素の順序は、初登場に基づいて保持されます。

例

以下の例は、異なる種類の配列で distinct 関数の使い方を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "numbers": [ 1, 2, 2, 3, 1 ],
    "names": [ "apple", "banana", "apple", "cherry" ],
    "resources": [
      {
        "name": "storage1",
        "type": "Microsoft.Storage/storageAccounts"
      },
      {
        "name": "vm1",
        "type": "Microsoft.Compute/virtualMachines"
      },
      {
        "name": "storage1",
        "type": "Microsoft.Storage/storageAccounts"
      }
    ]
  },
  "outputs": {
    "uniqueNumbers": {
      "type": "array",
      "value": "[distinct(variables('numbers'))]"
    },
    "uniqueNames": {
      "type": "array",
      "value": "[distinct(variables('names'))]"
    },
    "uniqueResources": {
      "type": "array",
      "value": "[distinct(variables('resources'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 タイプ 値
uniqueNumbers 配列 [1, 2, 3]
uniqueNames 配列 [「リンゴ」「バナナ」「チェリー」]
uniqueResources 配列 [{"name": "storage1", "type": "Microsoft.Storage/storageAccounts"}, {"name": "vm1", "type": "Microsoft.Compute/virtualMachines"}]

空

empty(itemToTest)

配列、オブジェクト、または文字列が空かどうかを判断します。

Bicep で、 empty 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
itemToTest はい 配列、オブジェクト、文字列 空かどうかを確認する値。

戻り値

値が空の場合は True を、値が空でない場合は False を返します。

例

次の例では、配列、オブジェクト、および文字列が空かどうかを確認します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "testArray": {
      "type": "array",
      "defaultValue": []
    },
    "testObject": {
      "type": "object",
      "defaultValue": {}
    },
    "testString": {
      "type": "string",
      "defaultValue": ""
    }
  },
  "resources": [
  ],
  "outputs": {
    "arrayEmpty": {
      "type": "bool",
      "value": "[empty(parameters('testArray'))]"
    },
    "objectEmpty": {
      "type": "bool",
      "value": "[empty(parameters('testObject'))]"
    },
    "stringEmpty": {
      "type": "bool",
      "value": "[empty(parameters('testString'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayEmpty ブール 正しい
objectEmpty ブール 正しい
stringEmpty ブール 正しい

まずは

first(arg1)

配列の最初の要素、または文字列の最初の文字を返します。

Bicep で、 first 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列または文字列 最初の要素または文字を取得する値。

戻り値

配列の最初の要素の型 (文字列、整数、配列、またはオブジェクト)、または文字列の最初の文字。

例

次の例は、配列と文字列で first 関数を使用する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "arrayOutput": {
      "type": "string",
      "value": "[first(parameters('arrayToTest'))]"
    },
    "stringOutput": {
      "type": "string",
      "value": "[first('One Two Three')]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayOutput 糸 1
stringOutput 糸 O

indexFromEnd

indexFromEnd(sourceArray, reverseIndex)

末尾から後方にカウントして、配列の要素を返します。 この関数は、リストの先頭ではなく末尾から要素を参照したい場合に便利です。 tryIndexFromEnd関数は、indexFromEndの安全なバージョンです。

Bicep では、 予約済みインデックス アクセサー 演算子を使用します。

パラメーター

パラメーター 必須 タイプ 説明
sourceArray はい アレイ 末尾から後方にカウントして要素を取得する値。
reverseIndex はい 整数 配列の末尾からの 1 から始まるインデックス。

戻り値

配列の末尾から後方にカウントして選択された、配列の 1 つの要素。

例

次の例は、indexFromEnd 関数の使用法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "items": [
      "apple",
      "banana",
      "orange",
      "grape"
    ]
  },
  "resources": [],
  "outputs": {
    "secondToLast": {
      "type": "string",
      "value": "[indexFromEnd(variables('items'), 2)]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
secondToLast 糸 オレンジ

indexOf

indexOf(arrayToSearch, itemToFind)

ある項目が配列内で最初に見つかった位置のインデックスの整数を返します。 文字列式の比較では大文字と小文字が区別されます。

パラメーター

パラメーター 必須 タイプ 説明
arrayToSearch はい アレイ 検索対象項目のインデックスを見つけるために使われる配列。
itemToFind はい 整数、文字列、配列、オブジェクト 配列内で見つける項目。

戻り値

配列内の項目の最初のインデックスを表す整数。 インデックスは 0 から始まります。 もしアイテムが見つからなければ、関数は-1を返します。

例

次の例では、indexOf 関数および lastIndexOf 関数の使用方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "names": [
      "one",
      "two",
      "three"
    ],
    "numbers": [
      4,
      5,
      6
    ],
    "collection": [
      "[variables('names')]",
      "[variables('numbers')]"
    ],
    "duplicates": [
      1,
      2,
      3,
      1
    ]
  },
  "resources": [],
  "outputs": {
    "index1": {
      "type": "int",
      "value": "[lastIndexOf(variables('names'), 'two')]"
    },
    "index2": {
      "type": "int",
      "value": "[indexOf(variables('names'), 'one')]"
    },
    "notFoundIndex1": {
      "type": "int",
      "value": "[lastIndexOf(variables('names'), 'Three')]"
    },
    "index3": {
      "type": "int",
      "value": "[lastIndexOf(variables('numbers'), 4)]"
    },
    "index4": {
      "type": "int",
      "value": "[indexOf(variables('numbers'), 6)]"
    },
    "notFoundIndex2": {
      "type": "int",
      "value": "[lastIndexOf(variables('numbers'), '5')]"
    },
    "index5": {
      "type": "int",
      "value": "[indexOf(variables('collection'), variables('numbers'))]"
    },
    "index6": {
      "type": "int",
      "value": "[indexOf(variables('duplicates'), 1)]"
    },
    "index7": {
      "type": "int",
      "value": "[lastIndexOf(variables('duplicates'), 1)]"
    }
  }
}

前の例からの出力は次のようになります。

名前 タイプ 値
index1 INT 1
index2 INT 0
index3 INT 0
index4 INT 2
index5 INT 1
index6 INT 0
index7 INT 3
notFoundIndex1 INT -1
notFoundIndex2 INT -1

交差点

intersection(arg1, arg2, arg3, ...)

パラメーターから共通の要素を持つ 1 つの配列またはオブジェクトを返します。

Bicep で、 intersection 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列またはオブジェクト 共通の要素の検索に使用する 1 番目の値。
arg2 はい 配列またはオブジェクト 共通の要素の検索に使用する 2 番目の値。
その他の引数 いいえ 配列またはオブジェクト 共通の要素の検索に使用するその他の値。

戻り値

共通の要素を持つ配列またはオブジェクト。

例

次の例では、intersection を配列およびオブジェクトと共に使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstObject": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c"
      }
    },
    "secondObject": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "z",
        "three": "c"
      }
    },
    "firstArray": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    },
    "secondArray": {
      "type": "array",
      "defaultValue": [ "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "objectOutput": {
      "type": "object",
      "value": "[intersection(parameters('firstObject'), parameters('secondObject'))]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[intersection(parameters('firstArray'), parameters('secondArray'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
objectOutput オブジェクト {"one": "a", "three": "c"}
arrayOutput 配列 [「二」「三」]

最後

last(arg1)

配列の最後の要素、または文字列の最後の文字を返します。

Bicep で、 last 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列または文字列 最後の要素または文字を取得する値。

戻り値

配列の最後の要素の型 (文字列、整数、配列、またはオブジェクト)、または文字列の最後の文字。

例

次の例は、配列と文字列で last 関数を使用する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "arrayOutput": {
      "type": "string",
      "value": "[last(parameters('arrayToTest'))]"
    },
    "stringOutput": {
      "type": "string",
      "value": "[last('One Two Three')]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayOutput 糸 3
stringOutput 糸 e

lastIndexOf

lastIndexOf(arrayToSearch, itemToFind)

ある項目が配列内で最後に見つかった位置のインデックスの整数を返します。 文字列式の比較では大文字と小文字が区別されます。

パラメーター

パラメーター 必須 タイプ 説明
arrayToSearch はい アレイ 検索対象項目のインデックスを見つけるために使われる配列。
itemToFind はい 整数、文字列、配列、オブジェクト 配列内で見つける項目。

戻り値

配列内の項目の最後のインデックスを表す整数。 インデックスは 0 から始まります。 項目が見つからない場合は、-1 が返されます。

例

次の例では、indexOf 関数および lastIndexOf 関数の使用方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "names": [
      "one",
      "two",
      "three"
    ],
    "numbers": [
      4,
      5,
      6
    ],
    "collection": [
      "[variables('names')]",
      "[variables('numbers')]"
    ],
    "duplicates": [
      1,
      2,
      3,
      1
    ]
  },
  "resources": [],
  "outputs": {
    "index1": {
      "type": "int",
      "value": "[lastIndexOf(variables('names'), 'two')]"
    },
    "index2": {
      "type": "int",
      "value": "[indexOf(variables('names'), 'one')]"
    },
    "notFoundIndex1": {
      "type": "int",
      "value": "[lastIndexOf(variables('names'), 'Three')]"
    },
    "index3": {
      "type": "int",
      "value": "[lastIndexOf(variables('numbers'), 4)]"
    },
    "index4": {
      "type": "int",
      "value": "[indexOf(variables('numbers'), 6)]"
    },
    "notFoundIndex2": {
      "type": "int",
      "value": "[lastIndexOf(variables('numbers'), '5')]"
    },
    "index5": {
      "type": "int",
      "value": "[indexOf(variables('collection'), variables('numbers'))]"
    },
    "index6": {
      "type": "int",
      "value": "[indexOf(variables('duplicates'), 1)]"
    },
    "index7": {
      "type": "int",
      "value": "[lastIndexOf(variables('duplicates'), 1)]"
    }
  }
}

前の例からの出力は次のようになります。

名前 タイプ 値
index1 INT 1
index2 INT 0
index3 INT 0
index4 INT 2
index5 INT 1
index6 INT 0
index7 INT 3
notFoundIndex1 INT -1
notFoundIndex2 INT -1

長さ

length(arg1)

配列内の要素、文字列内の文字、またはオブジェクト内のルート レベル プロパティの数を返します。

Bicep で、 length 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい array、string、または object 要素の数を取得するために使用する配列、文字の数を取得するために使用する文字列、またはルート レベル プロパティの数を取得するために使用するオブジェクト。

戻り値

整数。

例

次の例は、配列と文字列で length を使用する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "arrayToTest": {
      "type": "array",
      "defaultValue": [
        "one",
        "two",
        "three"
      ]
    },
    "stringToTest": {
      "type": "string",
      "defaultValue": "One Two Three"
    },
    "objectToTest": {
      "type": "object",
      "defaultValue": {
        "propA": "one",
        "propB": "two",
        "propC": "three",
        "propD": {
          "propD-1": "sub",
          "propD-2": "sub"
        }
      }
    }
  },
  "resources": [],
  "outputs": {
    "arrayLength": {
      "type": "int",
      "value": "[length(parameters('arrayToTest'))]"
    },
    "stringLength": {
      "type": "int",
      "value": "[length(parameters('stringToTest'))]"
    },
    "objectLength": {
      "type": "int",
      "value": "[length(parameters('objectToTest'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
配列の長さ int 3
stringLength int 13
objectLength int 4

この関数を配列と共に使用して、リソースを作成するときのイテレーション数を指定できます。 次の例では、 パラメーター siteNames は、Web サイトの作成時に使用する名前の配列を参照します。

"copy": {
  "name": "websitescopy",
  "count": "[length(parameters('siteNames'))]"
}

配列でこの関数を使用する方法の詳細については、「ARM テンプレートでのリソースの反復処理」を参照してください。

マックス / 最大

max(arg1)

整数の配列または整数のコンマ区切りリストから最大値を返します。

Bicep で、 max 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 整数の配列、または整数のコンマ区切りリスト 最大値を取得するコレクション。

戻り値

最大値を表す整数。

例

次の例は、配列と整数のリストで max を使用する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ 0, 3, 2, 5, 4 ]
    }
  },
  "resources": [],
  "outputs": {
    "arrayOutput": {
      "type": "int",
      "value": "[max(parameters('arrayToTest'))]"
    },
    "intOutput": {
      "type": "int",
      "value": "[max(0,3,2,5,4)]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayOutput int 5
intOutput int 5

分

min(arg1)

整数の配列または整数のコンマ区切りリストから最小値を返します。

Bicep で、 min 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 整数の配列、または整数のコンマ区切りリスト 最小値を取得するコレクション。

戻り値

最小値を表す整数。

例

次の例は、配列と整数のリストで min を使用する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ 0, 3, 2, 5, 4 ]
    }
  },
  "resources": [],
  "outputs": {
    "arrayOutput": {
      "type": "int",
      "value": "[min(parameters('arrayToTest'))]"
    },
    "intOutput": {
      "type": "int",
      "value": "[min(0,3,2,5,4)]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayOutput int 0
intOutput int 0

範囲

range(startIndex, count)

始点となる整数から、指定した数の項目が含まれる配列を作成します。

Bicep で、 range 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
startIndex はい INT 配列の最初の整数です。 startIndexとcountの合計は2,147,483,647を超えてはなりません。
数える はい INT 配列内の整数の数。 10,000までの非負整数でなければなりません。

戻り値

整数の配列。

例

次の例は、range 関数の使用法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "startingInt": {
      "type": "int",
      "defaultValue": 5
    },
    "numberOfElements": {
      "type": "int",
      "defaultValue": 3
    }
  },
  "resources": [],
  "outputs": {
    "rangeOutput": {
      "type": "array",
      "value": "[range(parameters('startingInt'),parameters('numberOfElements'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
rangeOutput 配列 [5, 6, 7]

スキップ

skip(originalValue, numberToSkip)

配列内の指定した数の要素の後にあるすべての要素から成る配列を返します。または、文字列内の指定した数の文字の後にあるすべての文字から成る文字列を返します。

Bicep で、 skip 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
originalValue はい 配列または文字列 スキップ対象の配列または文字列。
numberToSkip はい INT スキップする要素または文字の数。 この値が0以下の場合、関数は値内のすべての要素または文字を返します。 配列や文字列の長さより大きい場合は、関数は空の配列または文字列を返します。

戻り値

配列または文字列。

例

次の例では、配列内の指定した数の要素と、文字列内の指定した文字数をスキップします。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "testArray": {
      "type": "array",
      "defaultValue": [
        "one",
        "two",
        "three"
      ]
    },
    "elementsToSkip": {
      "type": "int",
      "defaultValue": 2
    },
    "testString": {
      "type": "string",
      "defaultValue": "one two three"
    },
    "charactersToSkip": {
      "type": "int",
      "defaultValue": 4
    }
  },
  "resources": [],
  "outputs": {
    "arrayOutput": {
      "type": "array",
      "value": "[skip(parameters('testArray'),parameters('elementsToSkip'))]"
    },
    "stringOutput": {
      "type": "string",
      "value": "[skip(parameters('testString'),parameters('charactersToSkip'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayOutput 配列 ["three"]
stringOutput 糸 2 つの 3

取る

take(originalValue, numberToTake)

配列または文字列を返します。 配列には配列の先頭から指定された数の要素が含まれます。 文字列には文字列の先頭から指定された数の文字が含まれます。

Bicep で、 take 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
originalValue はい 配列または文字列 要素の取得元となる配列または文字列。
numberToTake はい INT 取得する要素または文字の数。 この値が 0 以下である場合、空の配列または文字列が返されます。 指定された配列または文字列の長さを超過している場合は、その配列または文字列のすべての要素が返されます。

戻り値

配列または文字列。

例

次の例では、配列から指定された数の要素を取得し、文字列から文字を取得します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "testArray": {
      "type": "array",
      "defaultValue": [
        "one",
        "two",
        "three"
      ]
    },
    "elementsToTake": {
      "type": "int",
      "defaultValue": 2
    },
    "testString": {
      "type": "string",
      "defaultValue": "one two three"
    },
    "charactersToTake": {
      "type": "int",
      "defaultValue": 2
    }
  },
  "resources": [],
  "outputs": {
    "arrayOutput": {
      "type": "array",
      "value": "[take(parameters('testArray'),parameters('elementsToTake'))]"
    },
    "stringOutput": {
      "type": "string",
      "value": "[take(parameters('testString'),parameters('charactersToTake'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
arrayOutput 配列 [「一」、「二」]
stringOutput 糸 オン

tryGet

tryGet(itemToTest, keyOrIndex)

tryGet は、オブジェクトまたは配列内の存在しないプロパティまたはインデックスにアクセスしようとすると、配置エラーを回避するのに役立ちます。 指定したキーまたはインデックスが存在しない場合、 tryGet はエラーをスローする代わりに null を返します。

Bicep では、 safe-dereference 演算子を 使用します。

パラメーター

パラメーター 必須 タイプ 説明
itemToTest はい 配列、オブジェクト 参照するオブジェクトまたは配列。
keyOrIndex はい string、int 配列またはオブジェクトから取得するキーまたはインデックス。 オブジェクトのプロパティ名または配列のインデックス。

戻り値

キーやインデックスの値が存在する場合は返します。 キーやインデックスが欠けているか、境界外の場合はnullを返します。

例

次の例では、配列、オブジェクト、および文字列が空かどうかを確認します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "variables": {
    "users": {
      "name": "John Doe",
      "age": 30
    },
    "colors": [
      "red",
      "green"
    ]
  },
  "resources": [],
  "outputs": {
    "region": {
      "type": "string",
      "nullable": true,
      "value": "[tryGet(variables('users'), 'region')]"
    },
    "name": {
      "type": "string",
      "nullable": true,
      "value": "[tryGet(variables('users'), 'name')]"
    },
    "firstColor": {
      "type": "string",
      "nullable": true,
      "value": "[tryGet(variables('colors'), 0)]"
    }
  }
}

前の例からの出力は次のようになります。

名前 タイプ 値
リージョン 糸 (NULL)
名前 糸 ジョン・ドウ
firstColor 糸 赤い

tryIndexFromEnd

tryIndexFromEnd(sourceArray, reverseIndex)

tryIndexFromEnd関数は、indexFromEndの安全なバージョンです。 インデックスが範囲外の場合は、エラーをスローせずに末尾から後方にカウントすることで、配列から値を取得します。

Bicep では、 予約インデックス アクセサー 演算子と Safe 逆参照 演算子を使用します。

パラメーター

パラメーター 必須 タイプ 説明
sourceArray はい アレイ 末尾から後方にカウントして要素を取得する値。
reverseIndex はい 整数 配列の末尾からの 1 から始まるインデックス。

戻り値

インデックスが有効な場合 (配列の境界内)、その逆インデックスにある配列要素を返します。 インデックスが範囲外の場合は、エラーをスローする代わりに null を返します。

例

次の例は、tryIndexFromEnd 関数の使用法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "items": [
      "apple",
      "banana",
      "orange",
      "grape"
    ]
  },
  "resources": [],
  "outputs": {
    "secondToLast": {
      "type": "string",
      "value": "[tryIndexFromEnd(variables('items'), 2)]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
secondToLast 糸 オレンジ

次の例は、バインドされていないシナリオを示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "items": {
      "type": "array",
      "defaultValue": [
        "apple",
        "banana",
        "orange",
        "grape"
      ]
    }
  },
  "resources": {},
  "outputs": {
    "outOfBound": {
      "type": "string",
      "nullable": true,
      "value": "[tryIndexFromEnd(parameters('items'), 5)]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
outOfBound 糸 (null)

UNION

union(arg1, arg2, arg3, ...)

パラメーターからすべての要素を持つ 1 つの配列またはオブジェクトを返します。 配列の場合、重複する値は 1 回含められます。 オブジェクトの場合、重複するプロパティ名は 1 回だけ含められまれます。

Bicep で、 union 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列またはオブジェクト 要素の結合に使用される 1 番目の値。
arg2 はい 配列またはオブジェクト 要素の結合に使用される 2 番目の値。
その他の引数 いいえ 配列またはオブジェクト 要素の結合に使用されるその他の値。

戻り値

配列またはオブジェクト。

注釈

union functionでは、パラメーターのシーケンスを使用して、結果の順序と値を決定します。

配列の場合、関数により、最初のパラメーターの各要素が反復処理され、まだ存在していない場合は結果に追加されます。 その後、2 番目以降のパラメーター (ある場合) に対して処理が繰り返されます。 値が既に存在する場合は、配列内の以前の配置が保持されます。

オブジェクトの場合、最初のパラメーターのプロパティ名と値が結果に追加されます。 以降のパラメーターでは、結果に新しい名前 (ある場合) が追加されます。 後のパラメーターに同じ名前のプロパティがある場合、既存の値はその値で上書きされます。 プロパティの順序は保証されません。

union関数は、最上位の要素をマージするだけでなく、その中の入れ子になったオブジェクトを再帰的にマージします。 入れ子になった配列値はマージされません。 次のセクションの 2 番目の例を参照してください。

例

次の例では、union を配列およびオブジェクトと共に使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstObject": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c1"
      }
    },
    "secondObject": {
      "type": "object",
      "defaultValue": {
        "three": "c2",
        "four": "d",
        "five": "e"
      }
    },
    "firstArray": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    },
    "secondArray": {
      "type": "array",
      "defaultValue": [ "three", "four" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "objectOutput": {
      "type": "object",
      "value": "[union(parameters('firstObject'), parameters('secondObject'))]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[union(parameters('firstArray'), parameters('secondArray'))]"
    }
  }
}

前の例の既定値の出力は次のとおりです。

名前 タイプ 値
objectOutput オブジェクト {"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}
arrayOutput 配列 [「一」「二」「三」「四」]

次の例は、ディープ マージ機能を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "firstObject": {
      "property": {
        "one": "a",
        "two": "b",
        "three": "c1"
      },
      "nestedArray": [
        1,
        2
      ]
    },
    "secondObject": {
      "property": {
        "three": "c2",
        "four": "d",
        "five": "e"
      },
      "nestedArray": [
        3,
        4
      ]
    },
    "firstArray": [
      [
        "one",
        "two"
      ],
      [
        "three"
      ]
    ],
    "secondArray": [
      [
        "three"
      ],
      [
        "four",
        "two"
      ]
    ]
  },
  "resources": [],
  "outputs": {
    "objectOutput": {
      "type": "Object",
      "value": "[union(variables('firstObject'), variables('secondObject'))]"
    },
    "arrayOutput": {
      "type": "Array",
      "value": "[union(variables('firstArray'), variables('secondArray'))]"
    }
  }
}

前の例からの出力は次のようになります。

名前 タイプ 値
objectOutput オブジェクト {"property":{"one":"a","two":"b","three":"c2","four":"d","five":"e"},"nestedArray":[3,4]}
arrayOutput 配列 [["one","two"],["three"],["four","two"]]

入れ子された配列が統合された場合、 objectOutput.nestedArray の値は[1, 2, 3, 4]、 arrayOutput の値は[["one", "two", "three"]、["three", "four", "two"]]となります。

次のステップ