How to remove duplicated items in array on Twig

后端 未结 7 1044
-上瘾入骨i
-上瘾入骨i 2020-12-14 06:40

How to remove duplicated items in array on Twig?

I have array value in twig such as.

{{ set array = [\"testA         


        
7条回答
  •  攒了一身酷
    2020-12-14 07:37

    {% set keeper = {} %}
    {% set throwaway = [] %}
    {% for thing in yourSet.all() %}
    
      {% if thing.id not in throwaway %}
        {% set throwaway = throwaway|merge([thing.id]) %}
        {% set keeper = keeper|merge([{ slug: thing.slug, title: thing.title}]) %}
      {% endif %}
    
    {% endfor %}
    

    Modification on the accepted answer above. I needed to remove duplicates from a set and end up with an object of just slug and title.

提交回复
热议问题