从Twig对象中删除重复数据并将其合并。

huangapple go评论84阅读模式
英文:

Remove duplicate data from the Twig object and combine it

问题

在我的Twig模板中,我有一个列表,类似这样:

1985 - John
1974 - Lena
1985 - Sandra
1974 - Tom
1985 - Jess

年份可能会重复。所以我需要将我的列表合并成这样:

1985 - John, Sandra, Jess
1974 - Lena, Tom

在Twig模板引擎中是否可以实现这个?

英文:

In my Twig template i have list, something like this:

1985 - John
1974 - Lena
1985 - Sandra
1974 - Tom
1985 - Jess

The year can be duplicated. So i need combine my list like this:

1985 - John, Sandra, Jess
1974 - Lena, Tom

Is it possible to do this in the Twig template engine?

答案1

得分: 2

在Twig中,我们可以使用merge过滤器将数据添加到数组中。

复杂的部分:

  • 我们必须合并每年的名称,然后将其与主数组合并,年份作为键
  • 如果我们尝试将数字用作键,Twig会将键重置为数字(01等)

如果我们有这些数据:

{% set data = [
  {"year": 1985, "name": "John"},
  {"year": 1974, "name": "Lena"},
  {"year": 1985, "name": "Sandra"},
  {"year": 1974, "name": "Tom"},
  {"year": 1985, "name": "Jess"},
]
%}

我们可以循环遍历它,并创建一个数组,以年份作为键,名称作为值:

{% set groupedData = {} %}

{# 遍历数据 #}

{% for person in data -%}
    {# 年份必须转换为字符串,否则在数组中使用数值键 #}
    {% set year = "y" ~ person["year"] %}
    {% set name = person["name"] %}
  
    {% if year not in groupedData|keys %}
        {# 年份在数组中不存在,添加一个带有年份和一个名称的数组 #}
        {% set groupedData = groupedData|merge({(year): [name]}) %}
    {% else %}
        {# 年份在数组中存在,将名称添加到此年份的名称数组中 #}
        {% set newNamesForYear = attribute(groupedData, year)|merge([name]) %}
        {% set groupedData = groupedData|merge({(year): newNamesForYear}) %}
    {% endif %}
{% endfor %}

并显示结果:

{% for year, names in groupedData %}
    {# 去掉年份中的y并打印名称 #}
    {{ year|slice(1) }} - {{ names|join(', ') }}
{% endfor %}

以下是结果:

1985 - John, Sandra, Jess
1974 - Lena, Tom

在TwigFiddle上尝试

使用的资源:

英文:

In Twig, we can add data to an array with the merge filter.

The tricky parts:

  • we have to merge the names for each year and then merge it to the main array, with the year as the key
  • Twig resets the key to a number (0, 1, etc.) if we try to use a number as a key

If we have this data:

{% set data = [
  {"year": 1985, "name": "John"},
  {"year": 1974, "name": "Lena"},
  {"year": 1985, "name": "Sandra"},
  {"year": 1974, "name": "Tom"},
  {"year": 1985, "name": "Jess"},
]
%}

We can loop over it, and create an array, with each year as the key and the names as values:

{% set groupedData = {} %}

{# Loop over data #}

{% for person in data -%}
    {# the year must be casted as a string, otherwise it uses numeric keys in the array #}
    {% set year = "y" ~ person["year"] %}
    {% set name = person["name"] %}
  
    {% if year not in groupedData|keys %}
        {# year doesn't exist in the array, add a key with the year and an array with one name #}
        {% set groupedData = groupedData|merge({(year): [name]}) %}
    {% else %}
        {# year exists in the array, add the name to the array of names for this year #}
        {% set newNamesForYear = attribute(groupedData, year)|merge([name]) %}
        {% set groupedData = groupedData|merge({(year): newNamesForYear}) %}
    {% endif %}
{% endfor %}

And display the result:

{% for year, names in groupedData %}
    {# remove the y in the year and print the names #}
    {{ year|slice(1) }} - {{ names|join(', ') }}
{% endfor %}

Here is the result:

1985 - John, Sandra, Jess
1974 - Lena, Tom

Try it online on TwigFiddle.

Resources used:

huangapple
  • 本文由 发表于 2023年8月10日 21:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定