Allo
I want to access values in certain index positions nested in an attribute and do some arithmetic with the results.
Can anyone help?
This should render in template dev tool ok.
#I got data like this in an attribute:
{% set giraffesMatches = [[874,943,22,22],[955,938,30,30],[955,934,20,20]] %}
{{giraffesMatches}}
I want to sum the product of the 3rd and 4th values.
(22*22) + (30*30) + (20*20) = 1784
{{(22*22) + (30*30) + (20*20)}}
Actually they are always the same values so this would be ok too
(22**2) + (30**2) + (20**2) = 1784
{{(22*22) + (30*30) + (20*20)}}
I can access and sum the 3rd values like this:
{{giraffesMatches|sum(attribute='3')}}
Or return them in a loop like this:
{% for index in giraffesMatches -%}
{%- if not loop.last %}
{{- index[3] -}},
{%- else -%}
{{index[3]}}
{% endif -%}
{%- endfor %}
But I can't actually figure out how to sum the squares or products.
{% for index in giraffesMatches -%}
{%- if not loop.last %}
{{- index[3]**2 -}}+
{%- else -%}
{{index[3]**2}}
{% endif -%}
{%- endfor %}
It would also be handy for something related if I could do a max rather than a sum like this
giraffesMatches|sum(attribute='3')
giraffesMatches|max(attribute='3')