Possible to merge and sum the values of an array? I have a super table that has 4 columns. I'm trying to create an array based off one column (contentType) and merges to get only the unique values while taking the sum of a different column (points). Here is a screen shot:



Based off that screenshot I would have the following results:


Motivation-To-Achieve : 2
Emotional-Stability : 2
Openness : 4


I'm able to get the unique content types, however, I can't figure out how to take the sum of the points column. This is what I have so far:

{% set results = {} %}

{% for row in entry.surveyData %}
    {% set type = row.contentType %}
    {% set pts = row.points %}
    {% set results = results|merge({ (type) : {'name': type, 'points': pts } }) %}
{% endfor %}
What you need to do is to add the row.points to any existing points for each type, before merging – otherwise you'll just overwrite the existing type/attribute and its values (in other words, the |merge filter won't automatically merge number values inside an object).

Something like this should work – note the use of the |default filter, to fall back to 0 if the results[type] key doesn't exist:

{% set results = {} %}

{% for row in entry.surveyData %}
    {% set type = row.contentType %}
    {% set pts = row.points + results[type].points|default(0) %}
    {% set results = results|merge({ (type) : {'name': type, 'points': pts } }) %}
{% endfor %}