Skip to content

Commit 1e6f61f

Browse files
committed
add toObject lambda function
1 parent d678cfe commit 1e6f61f

File tree

2 files changed

+265
-10
lines changed

2 files changed

+265
-10
lines changed

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

Lines changed: 110 additions & 5 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/07/2023
88

99
---
1010
# Lambda functions for Bicep
@@ -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 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: 155 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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/07/2023
88
---
99

1010
# Lambda functions for ARM templates
@@ -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
```json
5454
{
@@ -160,7 +160,7 @@ An array.
160160

161161
### Example
162162

163-
The following example shows how to use the map function.
163+
The following example shows how to use the `map` function.
164164

165165
```json
166166
{
@@ -249,7 +249,7 @@ Any.
249249

250250
### Example
251251

252-
The following examples show how to use the reduce function.
252+
The following examples show how to use the `reduce` function.
253253

254254
```json
255255
{
@@ -355,7 +355,7 @@ An array.
355355

356356
### Example
357357

358-
The following example shows how to use the sort function.
358+
The following example shows how to use the `sort` function.
359359

360360
```json
361361
{
@@ -410,6 +410,156 @@ The output from the preceding example sorts the dog objects from the youngest to
410410
| ---- | ---- | ----- |
411411
| 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"]}] |
412412

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

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

0 commit comments

Comments
 (0)