I have some data in json
[
{
"group":"a",
"value":"10"
},
{
"group":"a",
"value":"11"
},
{
"group":"b",
"value":"2"
},
{
"group":"b",
"value":"3"
}
]
or as a table to make it easier to read...
group value
a 10
a 11
b 2
b 3
I would like to rank by group
to result in
group value rank
a 10 1
a 11 2
b 2 1
b 3 2
In other languages I would loop through the data with a counter that resets when there is a new group
value. I can range through the data but I can't get a counter working. In the below example it seems like the previous counter value isn't kept in the next iteration and so each value is 1.
{{ $counter := 1 }}
{{- range $index, $element := $data }}
{{ inline (gt $index 0) "," "" }}
{
"group" : "{{ .group }}",
"value" : "{{ .value }}",
"rank" : "{{ $counter }}"
{{ $counter := add $counter 1 }}
}
{{- end -}}