Search code examples
azureazure-resource-managerazure-bicep

Bicep - How to update a dictionary object


How to use bicep to update a dictionary object, like: original dict object:

var dict = {
  'a': {}
}

I have a array now: ['b', 'c'] and I want to update dict object like:

{
  'a': {}
  'b': {}
  'c': {}
}

Could I implement this with bicep?


Solution

  • It would be great if you could explain your use case as it is unclear what you are trying to achieve.

    There are few bicep functions you could use to achieve what you trying to do:

    // Existing dictionary
    var dict = { a: {} }
    
    // Existing array
    var array = [ 'b', 'c' ]
    
    // Convert array to object
    var arrayOfObjects = [for item in array: { '${item}': {} }]
    // => arrayOfObjects = [ { b: {} }, { c: {} } ]
    
    // Merge with existing 
    var merge = concat([ dict ], arrayOfObjects)
    // => merge = [ { a: {} }, { b: {} }, { c: {} } ]
    
    // Create a single object with all elements
    var newDict = reduce(merge, {}, (cur, next) => union(cur, next))
    // => newDict = { a: {}, b: {}, c: {} }