ARM 模板的数组函数

本文介绍用于处理数组的模板函数。

若要获取由某个值分隔的字符串值数组,请参阅 split。

小窍门

建议使用 Bicep,因为它提供与 ARM 模板相同的功能,并且语法更易于使用。 若要了解详细信息,请参阅 array 函数。

数组

array(convertToArray)

将值转换为数组。

在 Bicep 中,使用函数 array 。

参数

参数 必选 类型 DESCRIPTION
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 。

参数

参数 必选 类型 DESCRIPTION
arg1 是的 数组或字符串 要串联的第一个数组或字符串。
其他参数 否 数组或字符串 按顺序排列要进行串联的其他数组或字符串。

此函数可以采用任意数量的参数,并且可以接受参数的字符串或数组。 但是,不能同时为参数提供数组和字符串。 数组仅与其他数组连接。

返回值

由串联值构成的字符串或数组。

示例:

以下示例演示如何合并两个数组:

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

参数

参数 必选 类型 DESCRIPTION
容器 是的 数组、对象或字符串 包含要查找的值的值。
itemToFind 是的 字符串或整数 要查找的值。

返回值

如果函数找到了该项,则为真;否则,错误。

示例:

以下示例演示如何与不同类型的配合使用 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 数据类型。

参数

参数 必选 类型 DESCRIPTION
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()]"
    }
  }
}

上述示例中的默认值输出为:

名称 类型 价值
stringArray 数组 [“a”、“b”、“c”]
intArray 数组 [1, 2, 3]
objectArray 数组 [{“one”: “a”, “two”: “b”, “three”: “c”}]
arrayArray 数组 [[“one”, “two”, “three”]]
emptyArray 数组 []

distinct

distinct(arrayToModify)

返回一个新数组,输入数组中所有重复值都被移除。 该函数保持最初出现的顺序。

在 Bicep 中,使用函数 distinct 。

参数

参数 必选 类型 DESCRIPTION
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]
独特名称 数组 [“苹果”、“香蕉”、“樱桃”]
uniqueResources 数组 [{“name”: “storage1”, “type”: “Microsoft.Storage/storageAccounts“}, {”name“: ”vm1“, ”type“: ”Microsoft.Compute/virtualMachines“}]

空

empty(itemToTest)

确定数组、对象或字符串是否为空。

在 Bicep 中,使用函数 empty 。

参数

参数 必选 类型 DESCRIPTION
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 。

参数

参数 必选 类型 DESCRIPTION
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 字符串 一个
stringOutput 字符串 O

indexFromEnd

indexFromEnd(sourceArray, reverseIndex)

通过从末尾向后计数返回数组的元素。 当你想从列表末尾而非起始引用元素时,这个函数非常有用。 该 tryIndexFromEnd 函数是一个安全的版本 indexFromEnd。

在 Bicep 中,使用 保留索引访问器 运算符。

参数

参数 必选 类型 DESCRIPTION
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)

返回数组中第一次出现的项的索引的整数。 字符串比较区分大小写。

参数

参数 必选 类型 DESCRIPTION
arrayToSearch 是的 数组 用于查找所搜索项索引的数组。
itemToFind 是的 整数、字符串、数组或对象 要在数组中查找的项。

返回值

一个整数,表示数组中项的第一个索引。 该索引从零开始。 如果找不到该项,函数返回 -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 。

参数

参数 必选 类型 DESCRIPTION
arg1 是的 数组或对象 用于查找通用元素的第一个值。
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 。

参数

参数 必选 类型 DESCRIPTION
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 字符串 三
stringOutput 字符串 e

lastIndexOf

lastIndexOf(arrayToSearch, itemToFind)

返回数组中最后一次出现的项的索引的整数。 字符串比较区分大小写。

参数

参数 必选 类型 DESCRIPTION
arrayToSearch 是的 数组 用于查找所搜索项索引的数组。
itemToFind 是的 整数、字符串、数组或对象 要在数组中查找的项。

返回值

一个整数,表示数组中项的最后一个索引。 该索引从零开始。 如果未找到该项,则返回 -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 。

参数

参数 必选 类型 DESCRIPTION
arg1 是的 数组、字符串或对象 用于获取元素数的数组、用于获取字符数的字符串,或用于获取根级属性数的对象。

返回值

一个整数。

示例:

以下示例演示如何与数组和字符串一 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(arg1)

返回整数数组或逗号分隔的整数列表中的最大值。

在 Bicep 中,使用函数 max 。

参数

参数 必选 类型 DESCRIPTION
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 。

参数

参数 必选 类型 DESCRIPTION
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 。

参数

参数 必选 类型 DESCRIPTION
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 。

参数

参数 必选 类型 DESCRIPTION
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 。

参数

参数 必选 类型 DESCRIPTION
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 有助于避免在尝试访问对象或数组中不存在的属性或索引时发生部署失败。 如果指定的键或索引不存在,则返回 null, tryGet 而不是引发错误。

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

参数

参数 必选 类型 DESCRIPTION
itemToTest 是的 array、 object 要调查的对象或数组。
keyOrIndex 是的 字符串, int 要从数组或对象中检索的键或索引。 对象的属性名称或数组的索引。

返回值

如果键或索引存在,返回该值。 如果键或索引缺失或超出边界,则返回空。

示例:

以下示例检查数组、对象和字符串是否为空:

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

前述示例的输出为:

名称 类型 价值
区域 字符串 (空值)
姓名 字符串 无名氏
firstColor 字符串 红色

tryIndexFromEnd

tryIndexFromEnd(sourceArray, reverseIndex)

该 tryIndexFromEnd 函数是一个安全的版本 indexFromEnd。 它通过从末尾向后计数而不引发错误(如果索引不在范围外)来检索数组中的值。

在 Bicep 中,使用 保留索引访问器 运算符和 Safe dereference 运算符。

参数

参数 必选 类型 DESCRIPTION
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(arg1, arg2, arg3, ...)

返回包含参数中所有元素的单个数组或对象。 对于数组,仅包含重复值一次。 对于对象,仅包含重复属性名称一次。

在 Bicep 中,使用函数 union 。

参数

参数 必选 类型 DESCRIPTION
arg1 是的 数组或对象 用于联接元素的第一个值。
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”]]。

后续步骤