共用方式為


ARM 範本的陣列函式

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

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

提示

建議使用 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 式。

參數

參數 必要 類型 描述
參數 1 是的 陣列或字串 串連的第一個陣列或字串。
更多引數 陣列或字串 循序串連更多陣列或字串。

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

傳回值

串連值的字串或陣列。

範例

下列範例示範如何結合兩個陣列:

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

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

{
  "$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 是的 字串或整數 要尋找的值。

傳回值

找到項目則傳回 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 布爾
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 陣列 [[“one”, “two”, “three”]]
emptyArray 陣列 []

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 式。

參數

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

傳回值

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

範例

下列範例示範如何使用 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 繩子 一個
stringOutput 繩子 O

indexFromEnd

indexFromEnd(sourceArray, reverseIndex)

從結尾向後計算,傳回數組的專案。 當您想要從清單結尾而不是開頭參考元素時,這會很有用。 函 tryIndexFromEnd 式是的安全版本 indexFromEnd

在 Bicep 中,使用 保留索引存取子 運算符。

參數

參數 必要 類型 描述
sourceArray 是的 陣列 要藉由從結尾倒退計數來擷取專案的值。
reverseIndex 是的 整數 陣組結尾的一個起始索引。

傳回值

陣列中的單一元素,從陣列結尾向後計算來選取。

範例

下列範例示範如何使用 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 是的 整數、字串、陣列或物件 要在陣列中尋找的項目。

傳回值

代表陣列中第一次項目索引的整數。 索引以零為起始。 如果找不到該項目,則傳回 -1。

範例

下列範例說明如何使用 indexOflastIndexOf 函式:

{
  "$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 式。

參數

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

傳回值

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

範例

下列範例示範如何搭配陣列和物件使用 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 陣列 [“two”, “three”]

最後一

last(arg1)

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

在 Bicep 中,使用 函 last 式。

參數

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

傳回值

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

範例

下列範例示範如何使用 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 是的 整數、字串、陣列或物件 要在陣列中尋找的項目。

傳回值

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

範例

下列範例說明如何使用 indexOflastIndexOf 函式:

{
  "$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 式。

參數

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

傳回值

整數。

範例

下列範例示範如何搭配陣列和字串使用 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 是指建立網站時要使用的名稱陣列:

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

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

最大值

max(arg1)

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

在 Bicep 中,使用 函 max 式。

參數

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

傳回值

代表最大值的整數。

範例

下列範例示範如何搭配陣列和整數清單使用 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 式。

參數

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

傳回值

代表最小值的整數。

範例

下列範例示範如何搭配陣列和整數清單使用 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 的總和不得大於 2147483647。
計數 是的 整數 (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(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 繩子 兩個三

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 陣列 [“one”, “two”]
stringOutput 繩子

tryGet

tryGet(itemToTest, keyOrIndex)

tryGet 可協助您避免在嘗試存取物件或陣列中不存在的屬性或索引時發生部署失敗。 如果指定的索引鍵或索引不存在, tryGet 則傳回 null,而不是擲回錯誤。

在 Bicep 中,使用 safe-dereference 運算符。

參數

參數 必要 類型 描述
itemToTest 是的 array, object 要查看的物件或陣列。
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

tryndexFromEnd(sourceArray, reverseIndex)

tryIndexFromEnd 式是的安全版本 indexFromEnd。 如果索引超出範圍,它會從結尾向後計算,而不會擲回錯誤,以從陣列擷取值。

在 Bicep 中,使用 保留索引存取子 運算子和 Safe 取值 運算子。

參數

參數 必要 類型 描述
sourceArray 是的 陣列 要藉由從結尾倒退計數來擷取專案的值。
reverseIndex 是的 整數 陣組結尾的一個起始索引。

傳回值

如果索引有效(在陣列範圍內),則傳回位於該反向索引的陣列專案。 如果索引超出範圍,則傳回 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, ...)

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

在 Bicep 中,使用 函 union 式。

參數

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

傳回值

陣列或物件。

備註

union function 使用參數序列來判斷結果的順序和值。

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

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

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 物體 {“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 物體 {“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”]]。

下一步