用于 Azure Resource Manager 模板的数组和对象函数Array and object functions for Azure Resource Manager templates

Resource Manager 提供以下用于处理数组和对象的函数。Resource Manager provides several functions for working with arrays and objects.

若要获取由某个值分隔的字符串值数组,请参阅 splitTo get an array of string values delimited by a value, see split.

arrayarray

array(convertToArray)

将值转换为数组。Converts the value to an array.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
convertToArrayconvertToArray Yes 整数、字符串、数组或对象int, string, array, or object 要转换为数组的值。The value to convert to an array.

返回值Return value

一个数组。An array.

示例Example

以下示例模板演示如何对不同的类型使用 array 函数。The following example template shows how to use the array function with different types.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
intOutputintOutput ArrayArray [1][1]
stringOutputstringOutput ArrayArray ["efgh"]["efgh"]
objectOutputobjectOutput ArrayArray [{"a": "b", "c": "d"}][{"a": "b", "c": "d"}]

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/array.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/array.json

coalescecoalesce

coalesce(arg1, arg2, arg3, ...)

从参数中返回第一个非 null 值。Returns first non-null value from the parameters. 空字符串、空数组和空对象不为 null。Empty strings, empty arrays, and empty objects are not null.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int、string、array 或 objectint, string, array, or object 要测试是否为 null 的第一个值。The first value to test for null.
其他参数additional args No int、string、array 或 objectint, string, array, or object 要测试是否为 null 的其他值。Additional values to test for null.

返回值Return value

第一个非 null 参数的值,可以是字符串、整数、数组或对象。The value of the first non-null parameters, which can be a string, int, array, or object. 如果所有参数都为 null,则为 null。Null if all parameters are null.

示例Example

以下示例模板显示 coalesce 不同用法的输出。The following example template shows the output from different uses of coalesce.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "objectToTest": {
            "type": "object",
            "defaultValue": {
                "null1": null, 
                "null2": null,
                "string": "default",
                "int": 1,
                "object": {"first": "default"},
                "array": [1]
            }
        }
    },
    "resources": [
    ],
    "outputs": {
        "stringOutput": {
            "type": "string",
            "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').string)]"
        },
        "intOutput": {
            "type": "int",
            "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').int)]"
        },
        "objectOutput": {
            "type": "object",
            "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').object)]"
        },
        "arrayOutput": {
            "type": "array",
            "value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').array)]"
        },
        "emptyOutput": {
            "type": "bool",
            "value": "[empty(coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringOutputstringOutput StringString 默认值default
intOutputintOutput intInt 11
objectOutputobjectOutput ObjectObject {"first": "default"}{"first": "default"}
arrayOutputarrayOutput ArrayArray [1][1]
emptyOutputemptyOutput BoolBool TrueTrue

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/coalesce.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/coalesce.json

concatconcat

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

合并多个数组并返回串联的数组,或合并多个字符串值并返回串联的字符串。Combines multiple arrays and returns the concatenated array, or combines multiple string values and returns the concatenated string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或字符串array or string 要串联的第一个数组或字符串。The first array or string for concatenation.
其他参数additional arguments No 数组或字符串array or string 按顺序排列的串联的其他数组或字符串。Additional arrays or strings in sequential order for concatenation.

此函数可以使用任意数量的参数,并可接受字符串或数组作为参数。This function can take any number of arguments, and can accept either strings or arrays for the parameters. 但是,不能同时为参数提供数组和字符串。However, you can't provide both arrays and strings for parameters. 数组仅与其他数组连接。Arrays are only concatenated with other arrays.

返回值Return value

由串联值构成的字符串或数组。A string or array of concatenated values.

示例Example

以下示例模板演示如何组合两个数组。The following example template shows how to combine two arrays.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
returnreturn ArrayArray ["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]["1-1", "1-2", "1-3", "2-1", "2-2", "2-3"]

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/concat-array.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/concat-array.json

以下示例模板演示如何组合两个字符串值并返回串联的字符串。The following example template shows how to combine two string values and return a concatenated string.

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

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
concatOutputconcatOutput StringString prefix-5yj4yjf5mbg72prefix-5yj4yjf5mbg72

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/concat-string.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/concat-string.json

containscontains

contains(container, itemToFind)

检查数组是否包含某个值、某个对象是否包含某个键,或者某个字符串是否包含某个子字符串。Checks whether an array contains a value, an object contains a key, or a string contains a substring. 字符串比较区分大小写。The string comparison is case-sensitive. 但在测试某个对象是否包含某个键时,该比较不区分大小写。However, when testing if an object contains a key, the comparison is case-insensitive.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
containercontainer Yes 数组、对象或字符串array, object, or string 包含要查找的值的值。The value that contains the value to find.
itemToFinditemToFind Yes 字符串或整数string or int 要查找的值。The value to find.

返回值Return value

如果找到该项,则为 True;否则为 FalseTrue if the item is found; otherwise, False.

示例Example

以下示例模板演示如何对不同的类型使用 contains:The following example template shows how to use contains with different types:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringTruestringTrue BoolBool TrueTrue
stringFalsestringFalse BoolBool FalseFalse
objectTrueobjectTrue BoolBool TrueTrue
objectFalseobjectFalse BoolBool FalseFalse
arrayTruearrayTrue BoolBool TrueTrue
arrayFalsearrayFalse BoolBool FalseFalse

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/contains.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/contains.json

createarraycreatearray

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

从参数创建数组。Creates an array from the parameters.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 字符串、整数、数组或对象String, Integer, Array, or Object 数组中的第一个值。The first value in the array.
其他参数additional arguments No 字符串、整数、数组或对象String, Integer, Array, or Object 数组中的其他值。Additional values in the array.

返回值Return value

一个数组。An array.

示例Example

以下示例模板演示如何对不同的类型使用 createArra:The following example template shows how to use createArray with different types:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringArraystringArray ArrayArray ["a", "b", "c"]["a", "b", "c"]
intArrayintArray ArrayArray [1, 2, 3][1, 2, 3]
objectArrayobjectArray ArrayArray [{"one": "a", "two": "b", "three": "c"}][{"one": "a", "two": "b", "three": "c"}]
arrayArrayarrayArray ArrayArray [["one", "two", "three"]][["one", "two", "three"]]

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/createarray.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/createarray.json

emptyempty

empty(itemToTest)

确定数组、对象或字符串是否为空。Determines if an array, object, or string is empty.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
itemToTestitemToTest Yes 数组、对象或字符串array, object, or string 要检查是否为空的值。The value to check if it is empty.

返回值Return value

如果该值为空,则返回 True;否则返回 FalseReturns True if the value is empty; otherwise, False.

示例Example

以下示例模板检查某个数组、对象和字符串是否为空。The following example template checks whether an array, object, and string are empty.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayEmptyarrayEmpty BoolBool TrueTrue
objectEmptyobjectEmpty BoolBool TrueTrue
stringEmptystringEmpty BoolBool TrueTrue

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/empty.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/empty.json

firstfirst

first(arg1)

返回数组的第一个元素,或字符串的第一个字符。Returns the first element of the array, or first character of the string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或字符串array or string 要检索第一个元素或字符的值。The value to retrieve the first element or character.

返回值Return value

数组中第一个元素的类型(字符串、整数、数组或对象),或者字符串的第一个字符。The type (string, int, array, or object) of the first element in an array, or the first character of a string.

示例Example

以下示例模板演示如何对数组和字符串使用 first 函数。The following example template shows how to use the first function with an array and string.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput StringString oneone
stringOutputstringOutput StringString OO

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/first.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/first.json

intersectionintersection

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

返回包含参数中通用元素的单个数组或对象。Returns a single array or object with the common elements from the parameters.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或对象array or object 用于查找通用元素的第一个值。The first value to use for finding common elements.
arg2arg2 Yes 数组或对象array or object 用于查找通用元素的第二个值。The second value to use for finding common elements.
其他参数additional arguments No 数组或对象array or object 用于查找通用元素的其他值。Additional values to use for finding common elements.

返回值Return value

包含通用元素的数组或对象。An array or object with the common elements.

示例Example

以下示例模板演示如何对数组和对象使用 intersection:The following example template shows how to use intersection with arrays and objects:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
objectOutputobjectOutput 对象Object {"one": "a", "three": "c"}{"one": "a", "three": "c"}
arrayOutputarrayOutput ArrayArray ["two", "three"]["two", "three"]

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/intersection.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/intersection.json

jsonjson

json(arg1)

返回一个 JSON 对象。Returns a JSON object.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes stringstring 要转换为 JSON 的值。The value to convert to JSON.

返回值Return value

指定字符串中的 JSON 对象,或指定 null 时的空对象 。The JSON object from the specified string, or an empty object when null is specified.

备注Remarks

如需将某个参数值或变量包含在 JSON 对象中,可使用 concat 函数创建要传递到此函数的字符串。If you need to include a parameter value or variable in the JSON object, use the concat function to create the string that you pass to the function.

示例Example

以下示例模板演示如何对数组和对象使用 json 函数:The following example template shows how to use the json function with arrays and objects:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testValue": {
            "type": "string",
            "defaultValue": "demo value"
        }
    },
    "resources": [
    ],
    "outputs": {
        "jsonOutput": {
            "type": "object",
            "value": "[json('{\"a\": \"b\"}')]"
        },
        "nullOutput": {
            "type": "bool",
            "value": "[empty(json('null'))]"
        },
        "paramOutput": {
            "type": "object",
            "value": "[json(concat('{\"a\": \"', parameters('testValue'), '\"}'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
jsonOutputjsonOutput ObjectObject {"a": "b"}{"a": "b"}
nullOutputnullOutput 布尔Boolean TrueTrue
paramOutputparamOutput ObjectObject {"a": "demo value"}{"a": "demo value"}

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/json.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/json.json

lastlast

last (arg1)

返回数组的最后一个元素,或字符串的最后一个字符。Returns the last element of the array, or last character of the string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或字符串array or string 要检索最后一个元素或字符的值。The value to retrieve the last element or character.

返回值Return value

数组中最后一个元素的类型(字符串、整数、数组或对象),或者字符串的最后一个字符。The type (string, int, array, or object) of the last element in an array, or the last character of a string.

示例Example

以下示例模板演示如何对数组和字符串使用 last 函数。The following example template shows how to use the last function with an array and string.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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')]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput StringString threethree
stringOutputstringOutput StringString ee

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/last.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/last.json

lengthlength

length(arg1)

返回数组中的元素数、字符串中的字符数或对象中的根级属性数。Returns the number of elements in an array, characters in a string, or root-level properties in an object.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组、字符串或对象array, string, or object 用于获取元素数的数组、用于获取字符数的字符串,或用于获取根级属性数的对象。The array to use for getting the number of elements, the string to use for getting the number of characters, or the object to use for getting the number of root-level properties.

返回值Return value

一个整数。An int.

示例Example

以下示例模板演示如何对数组和字符串使用 length:The following example template shows how to use length with an array and string:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayLengtharrayLength intInt 33
stringLengthstringLength intInt 1313
objectLengthobjectLength intInt 44

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/length.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/length.json

创建资源时,可在数组中使用此函数指定迭代数。You can use this function with an array to specify the number of iterations when creating resources. 在以下示例中,参数 siteNames 引用创建网站时要使用的名称数组。In the following example, the parameter siteNames would refer to an array of names to use when creating the web sites.

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

有关在数组中使用此函数的详细信息,请参阅 Create multiple instances of resources in Azure Resource Manager(在 Azure Resource Manager 中创建多个资源实例)。For more information about using this function with an array, see Create multiple instances of resources in Azure Resource Manager.

maxmax

max(arg1)

返回整数数组或逗号分隔的整数列表中的最大值。Returns the maximum value from an array of integers or a comma-separated list of integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 整数数组或逗号分隔的整数列表array of integers, or comma-separated list of integers 要获取最大值的集合。The collection to get the maximum value.

返回值Return value

表示最大值的整数。An int representing the maximum value.

示例Example

以下示例模板演示如何对整数数组和整数列表使用 max:The following example template shows how to use max with an array and a list of integers:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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)]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput intInt 55
intOutputintOutput intInt 55

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/max.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/max.json

minmin

min(arg1)

返回整数数组或逗号分隔的整数列表中的最小值。Returns the minimum value from an array of integers or a comma-separated list of integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 整数数组或逗号分隔的整数列表array of integers, or comma-separated list of integers 要获取最小值的集合。The collection to get the minimum value.

返回值Return value

表示最小值的整数。An int representing the minimum value.

示例Example

以下示例模板演示如何对整数数组和整数列表使用 min:The following example template shows how to use min with an array and a list of integers:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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)]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput intInt 00
intOutputintOutput intInt 00

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/min.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/min.json

rangerange

range(startIndex, count)

从起始整数创建整数数组并包含一些项。Creates an array of integers from a starting integer and containing a number of items.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
startIndexstartIndex Yes intint 数组中的第一个整数。The first integer in the array. startIndex 和 count 的总和不得大于 2147483647。The sum of startIndex and count must be no greater than 2147483647.
countcount Yes intint 数组中的整数个数。The number of integers in the array. 必须为非负整数,最大为 10000。Must be non-negative integer up to 10000.

返回值Return value

整数数组。An array of integers.

示例Example

以下示例模板演示如何使用 range 函数:The following example template shows how to use the range function:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
rangeOutputrangeOutput ArrayArray [5, 6, 7][5, 6, 7]

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/range.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/range.json

skipskip

skip(originalValue, numberToSkip)

返回一个数组,其中包含数组中指定数字后面的所有元素;或返回一个字符串,其中包含字符串中指定数后面的所有字符。Returns an array with all the elements after the specified number in the array, or returns a string with all the characters after the specified number in the string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
originalValueoriginalValue Yes 数组或字符串array or string 用于跳过的数组或字符串。The array or string to use for skipping.
numberToSkipnumberToSkip Yes intint 要跳过的元素数或字符数。The number of elements or characters to skip. 如果此值小于或等于 0,则返回值中的所有元素或字符。If this value is 0 or less, all the elements or characters in the value are returned. 如果此值大于数组或字符串的长度,则返回空数组或字符串。If it is larger than the length of the array or string, an empty array or string is returned.

返回值Return value

数组或字符串。An array or string.

示例Example

以下示例模板跳过数组中指定数目的元素,以及字符串中指定数目的字符。The following example template skips the specified number of elements in the array, and the specified number of characters in a string.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput ArrayArray ["three"]["three"]
stringOutputstringOutput StringString two threetwo three

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/skip.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/skip.json

taketake

take(originalValue, numberToTake)

返回一个数组,其中包含从数组开头位置算起的指定数目的元素;或返回一个字符串,其中包含从字符串开头位置算起的指定数目的字符。Returns an array with the specified number of elements from the start of the array, or a string with the specified number of characters from the start of the string.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
originalValueoriginalValue Yes 数组或字符串array or string 要从中提取元素的数组或字符串。The array or string to take the elements from.
numberToTakenumberToTake Yes intint 要提取的元素或字符数。The number of elements or characters to take. 如果此值为 0 或更小,则返回一个空数组或字符串。If this value is 0 or less, an empty array or string is returned. 如果此值大于给定数组或字符串的长度,则返回数组或字符串中的所有元素。If it is larger than the length of the given array or string, all the elements in the array or string are returned.

返回值Return value

数组或字符串。An array or string.

示例Example

以下示例模板从数组中提取指定数目的元素,并从字符串中提取指定数目的字符。The following example template takes the specified number of elements from the array, and characters from a string.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput ArrayArray ["one", "two"]["one", "two"]
stringOutputstringOutput StringString onon

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/take.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/take.json

unionunion

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

返回包含参数中所有元素的单个数组或对象。Returns a single array or object with all elements from the parameters. 重复的值或键仅包含一次。Duplicate values or keys are only included once.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 数组或对象array or object 用于联接元素的第一个值。The first value to use for joining elements.
arg2arg2 Yes 数组或对象array or object 用于联接元素的第二个值。The second value to use for joining elements.
其他参数additional arguments No 数组或对象array or object 用于联接元素的其他值。Additional values to use for joining elements.

返回值Return value

数组或对象。An array or object.

示例Example

以下示例模板演示如何对数组和对象使用 union:The following example template shows how to use union with arrays and objects:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
objectOutputobjectOutput ObjectObject {"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}{"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}
arrayOutputarrayOutput ArrayArray ["one", "two", "three", "four"]["one", "two", "three", "four"]

要使用 Azure CLI 部署此示例模板,请使用:To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/union.json

要使用 PowerShell 部署此示例模板,请使用:To deploy this example template with PowerShell, use:

New-AzResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/union.json

后续步骤Next steps