Skip to content

Commit c34a448

Browse files
Merge pull request #197571 from mumian/0509-items
ARM: add the items() function
2 parents da884de + 9553cd9 commit c34a448

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

articles/azure-resource-manager/templates/template-functions-object.md

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Template functions - objects
33
description: Describes the functions to use in an Azure Resource Manager template (ARM template) for working with objects.
44
ms.topic: conceptual
5-
ms.date: 03/10/2022
5+
ms.date: 05/09/2022
66
---
77

88
# Object functions for ARM templates
@@ -163,6 +163,146 @@ The output from the preceding example with the default values is:
163163
| objectOutput | Object | {"one": "a", "three": "c"} |
164164
| arrayOutput | Array | ["two", "three"] |
165165

166+
## items
167+
168+
`items(object)`
169+
170+
Converts a dictionary object to an array.
171+
172+
In Bicep, use the [items](../bicep/bicep-functions-object.md#items).
173+
174+
### Parameters
175+
176+
| Parameter | Required | Type | Description |
177+
|:--- |:--- |:--- |:--- |
178+
| object |Yes |object |The dictionary object to convert to an array. |
179+
180+
### Return value
181+
182+
An array of objects for the converted dictionary. Each object in the array has a `key` property that contains the key value for the dictionary. Each object also has a `value` property that contains the properties for the object.
183+
184+
### Example
185+
186+
The following example converts a dictionary object to an array. For each object in the array, it creates a new object with modified values.
187+
188+
```json
189+
{
190+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
191+
"contentVersion": "1.0.0.0",
192+
"variables": {
193+
"copy": [
194+
{
195+
"name": "modifiedListOfEntities",
196+
"count": "[length(items(variables('entities')))]",
197+
"input": {
198+
"key": "[items(variables('entities'))[copyIndex('modifiedListOfEntities')].key]",
199+
"fullName": "[items(variables('entities'))[copyIndex('modifiedListOfEntities')].value.displayName]",
200+
"itemEnabled": "[items(variables('entities'))[copyIndex('modifiedListOfEntities')].value.enabled]"
201+
}
202+
}
203+
],
204+
"entities": {
205+
"item002": {
206+
"enabled": false,
207+
"displayName": "Example item 2",
208+
"number": 200
209+
},
210+
"item001": {
211+
"enabled": true,
212+
"displayName": "Example item 1",
213+
"number": 300
214+
}
215+
}
216+
},
217+
"resources": [],
218+
"outputs": {
219+
"modifiedResult": {
220+
"type": "array",
221+
"value": "[variables('modifiedListOfEntities')]"
222+
}
223+
}
224+
}
225+
```
226+
227+
The preceding example returns:
228+
229+
```json
230+
"modifiedResult": {
231+
"type": "Array",
232+
"value": [
233+
{
234+
"fullName": "Example item 1",
235+
"itemEnabled": true,
236+
"key": "item001"
237+
},
238+
{
239+
"fullName": "Example item 2",
240+
"itemEnabled": false,
241+
"key": "item002"
242+
}
243+
]
244+
}
245+
```
246+
247+
The following example shows the array that is returned from the items function.
248+
249+
```json
250+
{
251+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
252+
"contentVersion": "1.0.0.0",
253+
"variables": {
254+
"entities": {
255+
"item002": {
256+
"enabled": false,
257+
"displayName": "Example item 2",
258+
"number": 200
259+
},
260+
"item001": {
261+
"enabled": true,
262+
"displayName": "Example item 1",
263+
"number": 300
264+
}
265+
},
266+
"entitiesArray": "[items(variables('entities'))]"
267+
},
268+
"resources": [],
269+
"outputs": {
270+
"itemsResult": {
271+
"type": "array",
272+
"value": "[variables('entitiesArray')]"
273+
}
274+
}
275+
}
276+
```
277+
278+
The example returns:
279+
280+
```json
281+
"itemsResult": {
282+
"type": "Array",
283+
"value": [
284+
{
285+
"key": "item001",
286+
"value": {
287+
"displayName": "Example item 1",
288+
"enabled": true,
289+
"number": 300
290+
}
291+
},
292+
{
293+
"key": "item002",
294+
"value": {
295+
"displayName": "Example item 2",
296+
"enabled": false,
297+
"number": 200
298+
}
299+
}
300+
]
301+
}
302+
```
303+
304+
The items() function sorts the objects in the alphabetical order. For example, **item001** appears before **item002** in the outputs of the two preceding samples.
305+
166306
<a id="json"></a>
167307

168308
## json

articles/azure-resource-manager/templates/template-functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Resource Manager provides several functions for working with objects.
164164
* [createObject](template-functions-object.md#createobject)
165165
* [empty](template-functions-object.md#empty)
166166
* [intersection](template-functions-object.md#intersection)
167+
* [items](template-functions-object.md#items)
167168
* [json](template-functions-object.md#json)
168169
* [length](template-functions-object.md#length)
169170
* [null](template-functions-object.md#null)

0 commit comments

Comments
 (0)