I want to define a map like this
MyMap = {
"abc" = {
}
"pqr" = {
}
"key-1" = {
}
"key-2" = {
}
"key-3" = {
}
"key-4" = {
}
}
This is just an illustration but I can have many "key-n" keys. I was wondering if I could do some for loop magic to generate "key-n" keys dynamically while rest of the keys static. I am not finding any such examples. My goal is to find a way to do something like (below does not work)
MyMap = {
"abc" = {
}
"pqr" = {
}
for i in range(1, n) :
"key-${i}" => {
}
}
There is not any direct syntax for this but you can achieve the same result by merging two maps together, where one of them is written out literally and the other one is generated dynamically using an expression.
example = merge(
tomap({
"abc" = {
}
"pqr" = {
}
}),
tomap({
for i in range(1, n) : "key-${i}" => {
}
}),
}
References: