Skip to content

Commit 5f2e2b6

Browse files
authored
Merge pull request #226583 from mumian/0206-lambda-toobject
add toObject lambda function
2 parents 0350607 + b7d7be0 commit 5f2e2b6

File tree

2 files changed

+283
-22
lines changed

2 files changed

+283
-22
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.

0 commit comments

Comments
 (0)