Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T19:54:03.407Z
Answers.Text: 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?
Answers.UserDisplayName: Siebird
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T19:57:50.707Z
Answers.Text: 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 :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T20:01:28.347Z
Answers.Text: Fantastic! It's working. Thanks for the help Mats.
Answers.UserDisplayName: Siebird
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T20:06:55.750Z
Answers.Text: No problem. Great that it worked out :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T19:43:39.460Z
Answers.LastActivityDate: 2016-08-05T19:57:22.517Z
Answers.LastEditDate: 2016-08-05T19:57:22.517Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-05T19:20:25.907Z
Comments.Text: So you'd want the final sum to equal 8?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-05T19:43:37.180Z
Comments.Text: No, I have the desired results just below the screenshot.
Comments.UserDisplayName: Siebird
Tags: <templating><array><merge>
Title: Possible to merge and sum the values of an array?
ThreadText: # 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:
  
  [![enter image description here](https://i.sstatic.net/XtX75.png)](https://i.sstatic.net/XtX75.png)
  
  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 username_2.  
  > **username_2**: No problem. Great that it worked out :)  
  
  ---
  
  