<p>What you need to do is to add the <code>row.points</code> to any existing points for each <code>type</code>, before merging – otherwise you'll just overwrite the existing type/attribute and its values (in other words, the <code>|merge</code> filter won't automatically merge number values inside an object).</p>

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

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2016-08-05T19:54:03.407Z
Clever! However, getting a error on the front-end `Fatal error: Unsupported operand types in /../craft/app/vendor/twig/twig/lib/Twig/Environment.php(403) : eval()'d code on line 67`. Does this have to do with the `results[type]` being in brackets vs curly braces?
Siebird
CC BY-SA 3.0
2016-08-05T19:57:50.707Z
No – I just made a typo in my original answer. It should be `results[type].points`, not `results[type]` – sorry about that, see edit above :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-05T20:01:28.347Z
Fantastic! It's working. Thanks for the help Mats.
Siebird
CC BY-SA 3.0
2016-08-05T20:06:55.750Z
No problem. Great that it worked out :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-05T19:43:39.460Z
2016-08-05T19:57:22.517Z
2016-08-05T19:57:22.517Z
Mats Mikkel Rummelhoff
<p>I have a super table that has 4 columns. I'm trying to create an array based off one column (<code>contentType</code>) and merges to get only the unique values while taking the sum of a different column (<code>points</code>). Here is a screen shot:</p>

<p><a href="https://i.sstatic.net/XtX75.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XtX75.png" alt="enter image description here"></a></p>

<p>Based off that screenshot I would have the following results:</p>

<ul>
<li>Motivation-To-Achieve : 2</li>
<li>Emotional-Stability : 2</li>
<li>Openness : 4</li>
</ul>

<p>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:</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2016-08-05T19:20:25.907Z
So you'd want the final sum to equal 8?
Brad Bell
CC BY-SA 3.0
2016-08-05T19:43:37.180Z
No, I have the desired results just below the screenshot.
Siebird
<templating><array><merge>
Possible to merge and sum the values of an array?
# Possible to merge and sum the values of an array?

(Asked by: username_0 on 2016-08-05)

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 %}

**Tags:** templating, array, merge

> **Comments:**
> **username_1**: So you'd want the final sum to equal 8?  
> **username_0**: No, I have the desired results just below the screenshot.  

---

## 1 answers

### Answer by username_2 on 2016-08-05. Score: 3 | Accepted answer

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 %}

> **Comments:**
> **username_0**: Clever! However, getting a error on the front-end `Fatal error: Unsupported operand types in /../craft/app/vendor/twig/twig/lib/Twig/Environment.php(403) : eval()'d code on line 67`. Does this have to do with the `results[type]` being in brackets vs curly braces?  
> **username_2**: No – I just made a typo in my original answer. It should be `results[type].points`, not `results[type]` – sorry about that, see edit above :)  
> **username_0**: Fantastic! It's working. Thanks for the help Mats.  
> **username_2**: No problem. Great that it worked out :)  

---

