英文:
Transform ["a=b","c=d","e=f"] to {"a":"b","c":"d","e":"f"} in rego
问题
I have an array as defined below:
["dev=devA",
"instance=instanceA",
"domain=domainA",
"namespace=namespaceA",
"service=serviceA"]
I want an object in kv format, as defined below:
{"dev"="devA",
"instance"="instanceA",
"domain"="domainA",
"namespace"="namespaceA",
"service"="serviceA"}
I tried to {x | x := split(str, "=")[_]}
used sprintf
to print out. However, the sprintf
wants array[any]
, but the input is set[string]
.
英文:
I have an array as defined below
["dev=devA",
"instance=instanceA",
"domain=domainA",
"namespace=namespaceA",
"service=serviceA"]
I want an object in kv format, as defined below
{"dev"="devA",
"instance"="instanceA",
"domain"="domainA",
"namespace"="namespaceA",
"service="serviceA"}
I tried to {x | x := split(str, "/")[_]}
used sprintf
to print out. However, the sprintf
want array[any]
, now the input is set[string]
答案1
得分: 0
你可以使用对象理解(Object Comprehension):
package policy
arr := ["dev=devA",
"instance=instanceA",
"domain=domainA",
"namespace=namespaceA",
"service=serviceA"]
obj := {k: v |
item := arr[_]
[k, v] := split(item, "=")
}
完整示例请查看Rego Playground。
英文:
You could use an object comprehension:
package policy
arr := ["dev=devA",
"instance=instanceA",
"domain=domainA",
"namespace=namespaceA",
"service=serviceA"]
obj := {k: v |
item := arr[_]
[k, v] := split(item, "=")
}
Full example at the Rego Playground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论