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

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

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

ヒント

ARM テンプレートと同じ機能を備え、構文も使いやすいため、Bicep をお勧めします。 詳細については、アレイ 関数に関するページをご覧ください。

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'))]"
    }
  }
}

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

名前 Type
intOutput Array [1]
stringOutput Array ["efgh"]
objectOutput Array [{"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'))]"
    }
  }
}

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

名前 Type
戻り値 Array ["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]

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

{
  "$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))]"
    }
  }
}

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

名前 Type
concatOutput String prefix-5yj4yjf5mbg72

contains

contains(container, itemToFind)

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

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

パラメーター

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

戻り値

項目が見つかった場合は True、それ以外の場合は 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')]"
    }
  }
}

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

名前 Type
stringTrue Bool True
stringFalse Bool False
objectTrue Bool True
objectFalse Bool False
arrayTrue Bool True
arrayFalse Bool False

createArray

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

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

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

パラメーター

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

戻り値

配列。 パラメーターが指定されていない場合は、空の配列を返します。

次の例では、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()]"
    }
  }
}

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

名前 Type [値]
stringArray Array ["a", "b", "c"]
intArray Array [1, 2, 3]
objectArray Array [{"one": "a", "two": "b", "three": "c"}]
arrayArray Array [["one", "two", "three"]]
emptyArray Array []

empty

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'))]"
    }
  }
}

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

名前 Type
arrayEmpty Bool True
objectEmpty Bool True
stringEmpty Bool True

first

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')]"
    }
  }
}

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

名前 Type
arrayOutput String one
stringOutput String O

indexOf

indexOf(arrayToSearch, itemToFind)

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

パラメーター

パラメーター 必須 タイプ 説明
arrayToSearch はい array 検索対象項目のインデックスを見つけるために使われる配列。
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)]"
    }
  }
}

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

名前 Type
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

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'))]"
    }
  }
}

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

名前 Type
objectOutput Object {"one": "a", "three": "c"}
arrayOutput Array ["two", "three"]

last

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')]"
    }
  }
}

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

名前 Type
arrayOutput String three
stringOutput String e

lastIndexOf

lastIndexOf(arrayToSearch, itemToFind)

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

パラメーター

パラメーター 必須 タイプ 説明
arrayToSearch はい array 検索対象項目のインデックスを見つけるために使われる配列。
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)]"
    }
  }
}

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

名前 Type
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

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'))]"
    }
  }
}

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

名前 Type
arrayLength int 3
stringLength int 13
objectLength int 4

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

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

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

max

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)]"
    }
  }
}

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

名前 Type
arrayOutput int 5
intOutput int 5

min

min(arg1)

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

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

パラメーター

パラメーター 必須 タイプ 説明
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)]"
    }
  }
}

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

名前 Type
arrayOutput int 0
intOutput int 0

range

range(startIndex, count)

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

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

パラメーター

パラメーター 必須 タイプ 説明
startIndex はい INT 配列の最初の整数です。 startIndex と count の合計は、2147483647 より大きくてはいけません。
count はい INT 配列内の整数の数。 10000 までの負でない整数にする必要があります。

戻り値

整数の配列。

次の例では、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'))]"
    }
  }
}

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

名前 Type
rangeOutput Array [5, 6, 7]

skip

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'))]"
    }
  }
}

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

名前 Type
arrayOutput Array ["three"]
stringOutput String two three

take

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'))]"
    }
  }
}

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

名前 Type
arrayOutput Array ["one", "two"]
stringOutput String on

union

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

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

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

パラメーター

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

戻り値

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

注釈

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

配列の場合、関数により、最初のパラメーターの各要素が反復処理され、まだ存在していない場合は結果に追加されます。 その後、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'))]"
    }
  }
}

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

名前 Type
objectOutput Object {"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}
arrayOutput Array ["one", "two", "three", "four"]

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

{
  "$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'))]"
    }
  }
}

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

名前 Type
objectOutput Object {"property":{"one":"a","two":"b","three":"c2","four":"d","five":"e"},"nestedArray":[3,4]}
arrayOutput Array [["one","two"],["three"],["four","two"]]

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

次のステップ