Skip to content

Commit 8632ac1

Browse files
committed
add toObject
1 parent 21516d1 commit 8632ac1

File tree

2 files changed

+270
-14
lines changed

2 files changed

+270
-14
lines changed

articles/azure-resource-manager/bicep/bicep-functions-lambda.md

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Describes the lambda functions to use in a Bicep file.
44
author: mumian
55
ms.topic: conceptual
66
ms.author: jgao
7-
ms.date: 09/20/2022
7+
ms.date: 02/09/2023
88

99
---
1010
# Lambda functions for Bicep
@@ -22,7 +22,7 @@ This article describes the lambda functions to use in Bicep. [Lambda expressions
2222

2323
Bicep lambda function has these limitations:
2424

25-
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), and [`sort()`](#sort).
25+
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), [`sort()`](#sort), and [`toOrder()`](#toobject).
2626
- Using lambda variables (the temporary variables used in the lambda expressions) inside resource or module array access isn't currently supported.
2727
- Using lambda variables inside the [`listKeys`](./bicep-functions-resource.md#list) function isn't currently supported.
2828
- Using lambda variables inside the [reference](./bicep-functions-resource.md#reference) function isn't currently supported.
@@ -48,7 +48,7 @@ An array.
4848

4949
### Examples
5050

51-
The following examples show how to use the filter function.
51+
The following examples show how to use the `filter` function.
5252

5353
```bicep
5454
var dogs = [
@@ -120,7 +120,7 @@ An array.
120120

121121
### Example
122122

123-
The following example shows how to use the map function.
123+
The following example shows how to use the `map` function.
124124

125125
```bicep
126126
var dogs = [
@@ -187,7 +187,7 @@ Any.
187187

188188
### Example
189189

190-
The following examples show how to use the reduce function.
190+
The following examples show how to use the `reduce` function.
191191

192192
```bicep
193193
var dogs = [
@@ -263,7 +263,7 @@ An array.
263263

264264
### Example
265265

266-
The following example shows how to use the sort function.
266+
The following example shows how to use the `sort` function.
267267

268268
```bicep
269269
var dogs = [
@@ -298,6 +298,111 @@ The output from the preceding example sorts the dog objects from the youngest to
298298
| ---- | ---- | ----- |
299299
| dogsByAge | Array | [{"name":"Indy","age":2,"interests":["Butter"]},{"name":"Casper","age":3,"interests":["Other dogs"]},{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
300300

301+
## toObject
302+
303+
`toObject(inputArray, lambda expression, [lambda expression])`
304+
305+
Converts an array to an object with a custom key function and optional custom value function.
306+
307+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
308+
309+
### Parameters
310+
311+
| Parameter | Required | Type | Description |
312+
|:--- |:--- |:--- |:--- |
313+
| inputArray |Yes |array |The array used for creating an object.|
314+
| lambda expression |Yes |expression |The lambda expression used to provide the key predicate.|
315+
| lambda expression |No |expression |The lambda expression used to provide the value predicate.|
316+
317+
### Return value
318+
319+
An object.
320+
321+
### Example
322+
323+
The following example shows how to use the `toObject` function with the two required parameters:
324+
325+
```Bicep
326+
var dogs = [
327+
{
328+
name: 'Evie'
329+
age: 5
330+
interests: [ 'Ball', 'Frisbee' ]
331+
}
332+
{
333+
name: 'Casper'
334+
age: 3
335+
interests: [ 'Other dogs' ]
336+
}
337+
{
338+
name: 'Indy'
339+
age: 2
340+
interests: [ 'Butter' ]
341+
}
342+
{
343+
name: 'Kira'
344+
age: 8
345+
interests: [ 'Rubs' ]
346+
}
347+
]
348+
349+
output dogsObject object = toObject(dogs, entry => entry.name)
350+
```
351+
352+
The preceding example generates an object based on an array.
353+
354+
| Name | Type | Value |
355+
| ---- | ---- | ----- |
356+
| dogsObject | Object | {"Evie":{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},"Casper":{"name":"Casper","age":3,"interests":["Other dogs"]},"Indy":{"name":"Indy","age":2,"interests":["Butter"]},"Kira":{"name":"Kira","age":8,"interests":["Rubs"]}} |
357+
358+
The following `toObject` function with the third parameter provides the same output.
359+
360+
```Bicep
361+
output dogsObject object = toObject(dogs, entry => entry.name, entry => entry)
362+
```
363+
364+
The following example shows how to use the `toObject` function with three parameters.
365+
366+
```Bicep
367+
var dogs = [
368+
{
369+
name: 'Evie'
370+
properties: {
371+
age: 5
372+
interests: [ 'Ball', 'Frisbee' ]
373+
}
374+
}
375+
{
376+
name: 'Casper'
377+
properties: {
378+
age: 3
379+
interests: [ 'Other dogs' ]
380+
}
381+
}
382+
{
383+
name: 'Indy'
384+
properties: {
385+
age: 2
386+
interests: [ 'Butter' ]
387+
}
388+
}
389+
{
390+
name: 'Kira'
391+
properties: {
392+
age: 8
393+
interests: [ 'Rubs' ]
394+
}
395+
}
396+
]
397+
output dogsObject object = toObject(dogs, entry => entry.name, entry => entry.properties)
398+
```
399+
400+
The preceding example generates an object based on an array.
401+
402+
| Name | Type | Value |
403+
| ---- | ---- | ----- |
404+
| dogsObject | Object | {"Evie":{"age":5,"interests":["Ball","Frisbee"]},"Casper":{"age":3,"interests":["Other dogs"]},"Indy":{"age":2,"interests":["Butter"]},"Kira":{"age":8,"interests":["Rubs"]}} |
405+
301406
## Next steps
302407

303408
- See [Bicep functions - arrays](./bicep-functions-array.md) for additional array related Bicep functions.

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

Lines changed: 159 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ description: Describes the lambda functions to use in an Azure Resource Manager
44
author: mumian
55
ms.topic: conceptual
66
ms.author: jgao
7-
ms.date: 02/06/2023
7+
ms.date: 02/09/2023
88
---
99

1010
# Lambda functions for ARM templates
1111

12-
This article describes the lambda functions to use in ARM templates. [Lambda expressions (or lambda functions)](/dotnet/csharp/language-reference/operators/lambda-expressions) are essentially blocks of code that can be passed as an argument. They can take multiple parameters, but are restricted to a single line of code.
12+
This article describes the lambda functions to use in ARM templates. [Lambda functions](/dotnet/csharp/language-reference/operators/lambda-expressions) are essentially blocks of code that can be passed as an argument. They can take multiple parameters, but are restricted to a single line of code.
1313

1414
> [!TIP]
1515
> We recommend [Bicep](../bicep/overview.md) because it offers the same capabilities as ARM templates and the syntax is easier to use. To learn more, see [deployment](../bicep/bicep-functions-deployment.md) functions.
@@ -18,8 +18,8 @@ This article describes the lambda functions to use in ARM templates. [Lambda exp
1818

1919
ARM template lambda function has these limitations:
2020

21-
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), and [`sort()`](#sort).
22-
- Using lambda variables (the temporary variables used in the lambda expressions) inside resource or module array access isn't currently supported.
21+
- Lambda function can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), [`sort()`](#sort), and [`toObject()`](#toobject).
22+
- Using lambda variables (the temporary variables used in the lambda functions) inside resource or module array access isn't currently supported.
2323
- Using lambda variables inside the [`listKeys`](./template-functions-resource.md#list) function isn't currently supported.
2424
- Using lambda variables inside the [reference](./template-functions-resource.md#reference) function isn't currently supported.
2525

@@ -44,7 +44,7 @@ An array.
4444

4545
### Examples
4646

47-
The following examples show how to use the filter function.
47+
The following examples show how to use the `filter` function.
4848

4949
```json
5050
{
@@ -156,7 +156,7 @@ An array.
156156

157157
### Example
158158

159-
The following example shows how to use the map function.
159+
The following example shows how to use the `map` function.
160160

161161
```json
162162
{
@@ -245,7 +245,7 @@ Any.
245245

246246
### Example
247247

248-
The following examples show how to use the reduce function.
248+
The following examples show how to use the `reduce` function.
249249

250250
```json
251251
{
@@ -351,7 +351,7 @@ An array.
351351

352352
### Example
353353

354-
The following example shows how to use the sort function.
354+
The following example shows how to use the `sort` function.
355355

356356
```json
357357
{
@@ -406,6 +406,157 @@ The output from the preceding example sorts the dog objects from the youngest to
406406
| ---- | ---- | ----- |
407407
| dogsByAge | Array | [{"name":"Indy","age":2,"interests":["Butter"]},{"name":"Casper","age":3,"interests":["Other dogs"]},{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
408408

409+
## toObject
410+
411+
`toObject(inputArray, lambda expression, [lambda expression])`
412+
413+
Converts an array to an object with a custom key function and optional custom value function.
414+
415+
In Bicep, use the [toObject](../templates/template-functions-lambda.md#toobject) function.
416+
417+
### Parameters
418+
419+
| Parameter | Required | Type | Description |
420+
|:--- |:--- |:--- |:--- |
421+
| inputArray |Yes |array |The array used for creating an object.|
422+
| lambda expression |Yes |expression |The lambda expression used to provide the key predicate.|
423+
| lambda expression |No |expression |The lambda expression used to provide the value predicate.|
424+
425+
### Return value
426+
427+
An object.
428+
429+
### Example
430+
431+
The following example shows how to use the `toObject` function with the two required parameters:
432+
433+
```json
434+
{
435+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
436+
"contentVersion": "1.0.0.0",
437+
"variables": {
438+
"dogs": [
439+
{
440+
"name": "Evie",
441+
"age": 5,
442+
"interests": [
443+
"Ball",
444+
"Frisbee"
445+
]
446+
},
447+
{
448+
"name": "Casper",
449+
"age": 3,
450+
"interests": [
451+
"Other dogs"
452+
]
453+
},
454+
{
455+
"name": "Indy",
456+
"age": 2,
457+
"interests": [
458+
"Butter"
459+
]
460+
},
461+
{
462+
"name": "Kira",
463+
"age": 8,
464+
"interests": [
465+
"Rubs"
466+
]
467+
}
468+
]
469+
},
470+
"resources": {},
471+
"outputs": {
472+
"dogsObject": {
473+
"type": "object",
474+
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name))]"
475+
}
476+
}
477+
}
478+
```
479+
480+
The preceding example generates an object based on an array.
481+
482+
| Name | Type | Value |
483+
| ---- | ---- | ----- |
484+
| dogsObject | Object | {"Evie":{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},"Casper":{"name":"Casper","age":3,"interests":["Other dogs"]},"Indy":{"name":"Indy","age":2,"interests":["Butter"]},"Kira":{"name":"Kira","age":8,"interests":["Rubs"]}} |
485+
486+
The following `toObject` function with the third parameter provides the same output.
487+
488+
```json
489+
"outputs": {
490+
"dogsObject": {
491+
"type": "object",
492+
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name), lambda('entry', lambdaVariables('entry')))]"
493+
}
494+
}
495+
```
496+
497+
The following example shows how to use the `toObject` function with three parameters.
498+
499+
```json
500+
{
501+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
502+
"contentVersion": "1.0.0.0",
503+
"variables": {
504+
"dogs": [
505+
{
506+
"name": "Evie",
507+
"properties": {
508+
"age": 5,
509+
"interests": [
510+
"Ball",
511+
"Frisbee"
512+
]
513+
}
514+
},
515+
{
516+
"name": "Casper",
517+
"properties": {
518+
"age": 3,
519+
"interests": [
520+
"Other dogs"
521+
]
522+
}
523+
},
524+
{
525+
"name": "Indy",
526+
"properties": {
527+
"age": 2,
528+
"interests": [
529+
"Butter"
530+
]
531+
}
532+
},
533+
{
534+
"name": "Kira",
535+
"properties": {
536+
"age": 8,
537+
"interests": [
538+
"Rubs"
539+
]
540+
}
541+
}
542+
]
543+
},
544+
"resources": {},
545+
"outputs": {
546+
"dogsObject": {
547+
"type": "object",
548+
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name), lambda('entry', lambdaVariables('entry').properties))]"
549+
}
550+
}
551+
}
552+
```
553+
554+
The preceding example generates an object based on an array.
555+
556+
| Name | Type | Value |
557+
| ---- | ---- | ----- |
558+
| dogsObject | Object | {"Evie":{"age":5,"interests":["Ball","Frisbee"]},"Casper":{"age":3,"interests":["Other dogs"]},"Indy":{"age":2,"interests":["Butter"]},"Kira":{"age":8,"interests":["Rubs"]}} |
559+
409560
## Next steps
410561

411562
- See [Template functions - arrays](./template-functions-array.md) for additional array related template functions.

0 commit comments

Comments
 (0)