ARM 範本的陣列函式

本文說明處理陣列的範本函式。

若要取得以值分隔的字串值陣列,請參閱 分割

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入瞭解,請參閱陣列函式。

陣列

array(convertToArray)

將值轉換為陣列。

在 Bicep 中,使用 array 函式。

參數

參數 必要 類型​ 描述
convertToArray Yes 整數、字串、陣列或物件 要轉換為陣列的值。

傳回值

陣列。

範例

下列範例顯示如何使用不同類型的陣列函式。

{
  "$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 Yes 陣列或字串 串連的第一個陣列或字串。
更多引數 No 陣列或字串 循序串連更多陣列或字串。

此函式可以接受任意數目的引數,並且可針對參數接受字串或陣列。 但您無法為參數提供陣列和字串。 陣列只能與其他陣列串連。

傳回值

串連值的字串或陣列。

範例

下一個範例顯示如何結合兩個陣列。

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

上述範例中具有預設值的輸出如下:

名稱 類型
return 陣列 ["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]

下列範例顯示如何結合兩個字串值並傳回串連字串。

{
  "$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 String prefix-5yj4yjf5mbg72

contains

contains(container, itemToFind)

檢查陣列中是否包含值、物件中是否包含索引鍵,或字串中是否包含子字串。 字串比較會區分大小寫。 不過,測試時,如果物件包含索引鍵,比較便不區分大小寫。

在 Bicep 中,使用 contains 函式。

參數

參數 必要 類型​ 描述
容器 Yes 陣列、物件或字串 其中包含要尋找之值的值。
itemToFind Yes 字串或整數 要尋找的值。

傳回值

找到項目則傳回 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')]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
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 No 字串、整數、陣列或物件 陣列中的值。

傳回值

陣列。 如果未提供任何參數,則會傳回空陣列。

範例

下列範例顯示如何使用不同類型的 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 陣列 [["one", "two", "three"]]
emptyArray 陣列 []

empty

empty(itemToTest)

判斷陣列、物件或字串是否空白。

在 Bicep 中,使用 empty 函式。

參數

參數 必要 類型​ 描述
itemToTest Yes 陣列、物件或字串 檢查其是否為空白的值。

傳回值

如果值空白則傳回 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 Bool True
objectEmpty Bool True
stringEmpty Bool True

第一

first(arg1)

傳回陣列的第一個元素或字串的第一個字元。

在 Bicep 中,使用 first 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 陣列或字串 要擷取其第一個元素或字元的值。

傳回值

陣列中第一個元素的類型 (字串、整數、陣列或物件) 或字串的第一個字元。

範例

下列範例顯示如何搭配使用 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 String 一個
stringOutput String O

indexOf

indexOf(arrayToSearch, itemToFind)

傳回陣列中項目第一次出現索引的整數。 比較會針對字串區分大小寫

參數

參數 必要 類型​ 描述
arrayToSearch Yes 陣列 用於尋找搜尋項目索引的陣列。
itemToFind Yes 整數、字串、陣列或物件 要在陣列中尋找的項目。

傳回值

代表陣列中第一次項目索引的整數。 索引以零為起始。 如果找不到該項目,則傳回 -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, ...)

從參數中傳回具有共同元素的單一陣列或物件。

在 Bicep 中,使用 intersection 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 陣列或物件 要用來尋找共同元素的第一個值。
arg2 Yes 陣列或物件 要用來尋找共同元素的第二個值。
更多引數 No 陣列或物件 更多用於尋找共同元素的值。

傳回值

具有共同元素的陣列或物件。

範例

下列範例顯示如何搭配陣列和物件使用交集。

{
  "$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 Object {"one": "a", "three": "c"}
arrayOutput 陣列 ["two", "three"]

最後一

last(arg1)

傳回陣列的最後一個元素或字串的最後一個字元。

在 Bicep 中,使用 last 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 陣列或字串 要擷取其最後一個元素或字元的值。

傳回值

陣列中最後一個元素的類型 (字串、整數、陣列或物件) 或字串的最後一個字元。

範例

下列範例顯示如何搭配使用 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 String 3
stringOutput String e

lastIndexOf

lastIndexOf(arrayToSearch, itemToFind)

傳回陣列中項目最後一次出現索引的整數。 比較會針對字串區分大小寫

參數

參數 必要 類型​ 描述
arrayToSearch Yes 陣列 用於尋找搜尋項目索引的陣列。
itemToFind Yes 整數、字串、陣列或物件 要在陣列中尋找的項目。

傳回值

代表陣列中最後一次項目索引的整數。 索引以零為起始。 如果找不到該項目,則傳回 -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

length(arg1)

傳回陣列中的元素、字串中的字元,或物件中的根層級屬性的數目。

在 Bicep 中,使用 length 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 陣列、字串或物件 用於取得元素數目的陣列、用於取得字元數目的字串,或用於取得根層級屬性數目的物件。

傳回值

整數。

範例

下列範例顯示使用 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'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
arrayLength int 3
stringLength int 13
objectLength int 4

建立資源時,您可在陣列中使用此函式指定反覆運算的數量。 下列範例中,參數 siteNames 會參考在建立網站時要使用的名稱陣列。

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

如需搭配陣列來使用此函式的詳細資訊,請參閱 ARM 範本中的資源反覆運算

max

max(arg1)

傳回整數陣列的最大值,或以逗號分隔的整數清單。

在 Bicep 中,使用 max 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 整數的陣列,或以逗號分隔的整數清單 要用來取得最大值的集合。

傳回值

代表最大值的整數。

範例

下列範例說明在使用 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 Yes 整數的陣列,或以逗號分隔的整數清單 要用來取得最小值的集合。

傳回值

代表最小值的整數。

範例

下列範例顯示使用 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

range(startIndex, count)

從起始整數建立整數陣列,且其中包含一些項目。

在 Bicep 中,使用 range 函式。

參數

參數 必要 類型​ 描述
startIndex Yes int 陣列中的第一個整數。 startIndex 和 count 的總和不得大於 2147483647。
計數 Yes 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'))]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
rangeOutput 陣列 [5, 6, 7]

skip

skip(originalValue, numberToSkip)

傳回陣列中位於指定數字之後的所有元素所形成的陣列,或傳回字串中位於指定數字之後的所有字元所組成的字串。

在 Bicep 中,使用 skip 函式。

參數

參數 必要 類型​ 描述
originalValue Yes 陣列或字串 要用於略過的陣列或字串。
numberToSkip Yes 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 String two three

take

take(originalValue, numberToTake)

傳回陣列或字串。 陣列從其開頭算起,含有指定數目的元素。 字串從其開頭算起,含有指定數目的字元。

在 Bicep 中,使用 take 函式。

參數

參數 必要 類型​ 描述
originalValue Yes 陣列或字串 要從其中擷取元素的陣列或字串。
numberToTake Yes 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 陣列 ["one", "two"]
stringOutput String on

union

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

從參數中傳回具有所有元素的單一陣列或物件。 針對陣列,會包括重複的值一次。 針對物件,只會包括重複的屬性名稱一次。

在 Bicep 中,使用 union 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 陣列或物件 用來聯結元素的第一個值。
arg2 Yes 陣列或物件 用來聯結元素的第二個值。
更多引數 No 陣列或物件 可用於聯結元素的其他值。

傳回值

陣列或物件。

備註

Union 函式會使用參數的順序來決定結果的順序和值。

針對陣列,此函式會逐一查看第一個參數中的每個元素;若其尚未出現在結果中,便會將其新增至結果。 接著,系統會對第二個參數和任何更多參數重複上述程序。 如果值已經存在,則會保留較早放置於陣列中的值。

針對物件,會將來自第一個參數的屬性名稱和值新增至結果。 針對較後面的參數,則會將所有新名稱新增至結果。 如果較後面的參數具有相同名稱的屬性,該值就會覆寫現有的值。 無法保證屬性的順序。

union 函式不僅會合併最上層元素,而且會以遞迴方式合併其中的任何巢狀物件。 不會合併巢狀陣列值。 請參閱下一節中的第二個範例。

範例

下列範例顯示使用 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 Object {"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}
arrayOutput 陣列 ["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'))]"
    }
  }
}

前述範例的輸出為:

名稱 類型
objectOutput Object {"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"]]。

下一步