admin管理员组

文章数量:1399251

Given these two variables:

var businessUnits = ['bu1', 'bu2']
var environments = ['dev', 'prd']

I want to create new array with these values:

['bu1-dev', 'bu1-prd', 'bu2-dev', 'bu2-prd']

How to achieve this in bicep?

Given these two variables:

var businessUnits = ['bu1', 'bu2']
var environments = ['dev', 'prd']

I want to create new array with these values:

['bu1-dev', 'bu1-prd', 'bu2-dev', 'bu2-prd']

How to achieve this in bicep?

Share Improve this question edited Mar 26 at 19:20 Thomas 30k6 gold badges98 silver badges141 bronze badges Recognized by Microsoft Azure Collective asked Mar 26 at 18:05 joostmnljoostmnl 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can use lambda functions map and reduce to merge/combine arrays.

See my original answer for more details: How to loop over multiple arrays for a resource

param businessUnits array = ['bu1', 'bu2']
param environments array = ['dev', 'prd']

output buEnvs array = reduce(
  businessUnits,
  [],
  (cur, next) => concat(cur, map(environments, environment => '${next}-${environment}'))
)

As per MARCH 2025 this won't work with variables in bicep current state as variables are not typed. If you upgrade to latest bicep version (>= v0.34.1) There is an experimental feature for typed variabes.

You would need to create bicepconfig.json config file (see documentation):

{
  "experimentalFeaturesEnabled": {
    "typedVariables": true
  }
}

Then you would be able to use the same approach with variables:

var businessUnits array = ['bu1', 'bu2']
var environments array = ['dev', 'prd']

output buEnvs array = reduce(
  businessUnits,
  [],
  (cur, next) => concat(cur, map(environments, environment => '${next}-${environment}'))
)

本文标签: azureHow can i combine two arrays in bicepStack Overflow