jenny
jenny

Reputation: 79

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?

Upvotes: 4

Views: 1986

Answers (1)

Thomas
Thomas

Reputation: 29482

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: {} }

Upvotes: 6

Related Questions