First a FYI though, this is not something you should be doing in the template, cause the code is pretty messy. Anyway here is how you merge/group arrays in twig
:
{% set output = [] %}
{% for item in items %}
{% if not attribute(output, item.object.category) is defined %}
{% set output = output|merge({ (item.object.category) : {}, }) %}
{% endif %}
{% set output = output|merge({(item.object.category) : output[item.object.category] | merge([ item, ]) }) %}
{% endfor %}
{% for category, items in output %}
{{ category }}:
{% for item in items %}
{{ item.object.name }}
{% endfor %}
{% endfor %}