Home > Blockchain >  Bicep - How to create array of objects through loop with creating 2 or more objects each iteration
Bicep - How to create array of objects through loop with creating 2 or more objects each iteration

Time:10-29

To simplify my question. I have an example like this. Here is an array of objects I would like to create through a loop. All objects have both names and values as '0'. The array has 4 objects.

var result = [
  {
    'name': '0'
    'value': '0'
  }
  {
    'name': '0'
    'value': '0'
  }
  {
    'name': '0'
    'value': '0'
  }
  {
    'name': '0'
    'value': '0'
  }
]

here is the solution that has been verified working. It loops 4 times to create an array of objects. As a result, each Iteration creates 1 object only.

#loop 4 times
var result = [for i in range(0, 3): { 
  name: '0'
  value: '0'
}]

However, let say I only want to make the above result by a loop with fewer iterations (let say 2 iterations only). Therefore with each iteration, I have to create an array of 2 objects

Here is the code I tried but not working, but through it, you may understand what I try to get

#loop 2 times only
var result = [for i in range(0, 1): 
{
  name: '0'
  value: '0'
}
{
  name: '0'
  value: '0'
}]

Note that I have tried with many other ways (not only the one above) but not working (etc: using union function). I always get syntax errors or something else. Therefore I wonder if bicep has the capability to do what I want to achieve. Would anyone help me with this Thanks and best regards

CodePudding user response:

Azure Bicep Known Limitations:

  • No support for single-line object and arrays (i.e. ['a', 'b', 'c']) (#586).
  • Bicep is newline sensitive. We are exploring ways we can remove/relax this restriction (#146)

You can refer to Improve parser for more single-line array cases to improve error message , Improve error message when bicep array syntax is wrong and [top level array resource is not parsing. Error BCP018: Expected the "]" character at this location

  • Related