0
{% set valuesCars = ['bmw', 'mercedes', 'seat', 'porsche', 'seat', 'bmw', 'seat'] %}

{% set valuesIntegers = [27, 32, 36, 36, 27, 32, 32] %}

{% set counts = {} %}

{# Change valuesCars to valuesIntegers to output the integers #}
{% for value in valuesCars %}
    {% if counts[value] is not defined %}
        {% set counts = counts|merge({ (value): 1 }) %}
    {% else %}
        {% set counts = counts|merge({ (value): counts[value] + 1 }) %}
    {% endif %}
{% endfor %}

{% for key, count in counts %}
    {{ key }}: {{ count }}
{% endfor %}

For valuesCars I get the correct output:
bmw: 2
mercedes: 1
seat: 3
porsche: 1

For valuesIntegers I don't get the desired output. I get this:
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1

The desired output should be
27 : 2
32: 3
36 : 2

What am I'm missing here?

4
  • 1
    Please read the part about merging numeric indices and the issue attached. A "workaround" is also included in the answer, have a look at the part where I make the numeric index into a string with 'id_'~raw._id.id Commented Sep 25, 2024 at 5:58
  • @DarkBee : Thanks for your responses. I looked into your question on stackoverflow and your issue on github, this helped me to find a solution. Kind of hacky in my opinion. But strange behavior for the numeric index 🤷‍♂️ Commented Sep 25, 2024 at 20:01
  • Closing this question, does this mean other users of stackoverflow won't see the question and answer anymore? Commented Sep 25, 2024 at 21:06
  • 1
    No - When a question is closed as a duplicate that doesn't mean this question will be deleted. The system is there so people get pointed in the right direction with different search queries Commented Sep 26, 2024 at 4:55

2 Answers 2

0

This is the solution:

Convert the numeric key value into a string key to avoid issues with numeric indices

{% set stringKey = 'value_' ~ value %}

This results in:

value_27
value_32
value_36
value_36
value_27
value_32
value_32

Check if the key exists and update its count, else set the count to 1

{% if attribute(valuesWithNewKey, stringKey) is defined %}
    {% set valuesWithNewKey = valuesWithNewKey|merge({ (stringKey): valuesWithNewKey[stringKey] + 1 }) %}
{% else %}
    {% set valuesWithNewKey = valuesWithNewKey|merge({ (stringKey): 1 }) %}
{% endif %}

Inside the for loop (value in valuesIntegers) when you print the stringKey and values you will get:

{% for value in valuesWithNewKey %}
    {{ value }}<br>
{% endfor %}

value_27 : 1
value_32 : 1
value_32 : 1
value_36 : 1
value_36 : 1
value_36 : 1
value_36 : 1
value_36 : 1
value_36 : 2
value_27 : 2
value_27 : 1
value_27 : 2
value_32 : 2
value_32 : 2
value_32 : 2
value_32 : 2
value_32 : 3
value_32 : 2

Outside the for loop (value in valuesIntegers) after, replacing the 'value_' it will result in the desired output 🙏 💪 🏆

{% for stringKey, count in valuesWithNewKey %}
   {{ stringKey|replace({'value_':''}) ~ ' : ' ~ count }}<br>
{% endfor %}

27 : 2
32 : 3
36 : 2

The whole code:

 {% set valuesIntegers = [27, 32, 36, 36, 27, 32, 32] %}
 {% set valuesWithNewKey = {} %}

{% for value in valuesIntegers %}
   {% set stringKey = 'value_' ~ value %}

   {% if attribute(valuesWithNewKey, stringKey) is defined %}
     {% set valuesWithNewKey = valuesWithNewKey|merge({ (stringKey): valuesWithNewKey[stringKey] + 1 }) %}
   {% else %}
     {% set valuesWithNewKey = valuesWithNewKey|merge({ (stringKey): 1 }) %}
   {% endif %}
{% endfor %}

{% for stringKey, count in valuesWithNewKey %}
   {{ stringKey|replace({'value_':''}) ~ ' : ' ~ count }}<br>
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

-2

used inside the set counts structure.

{% set valuesIntegers = [27, 32, 36, 36, 27, 32, 32] %}

{% set counts = {} %}

{% for value in valuesIntegers %}
    {% if counts[value] is not defined %}
        {% set counts = counts|merge({ (value): 1 }) %}
    {% else %}
        {% set counts = counts|merge({ (value): counts[value] + 1 }) %}
    {% endif %}
{% endfor %}

{% for key, count in counts %}
    {{ key }}: {{ count }}
{% endfor %}

1 Comment

Did you actually test this answer before posting or read the comments I've posted beneath the question? You can't use merge with numeric indices

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.