Skip to content

how to cumulate elements in array of objects #241

Discussion options

You must be logged in to vote

I resolve this issue with following code:

result[key] := value {
    keys := { key | container := input.containers[i]; _ = container[key]}
    key := keys[_]
   # canonify_value is the function to convert string-value to number.
    values := [val | val := canonify_value(input.containers[_][key],key)]
    value := sum(values)
}

The core idea is convert input.containers to the following object:

{
  "cpu": [100, 25],
  "memory": [209715200000, 419430400000]
}

and then use build-in function sum to sum elements of array.

Is there any better resolution to resolve such problem with rego?

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@anderseknert
Comment options

@quanguachong
Comment options

Answer selected by quanguachong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants