如何输出最高分和与之相关的名字——液体逻辑

How to output highest score and name associated with it - liquid logic

我需要输出 highest_score 和与之关联的名称。代码输出 highest_score 但它没有与正确的名称相关联。我想它需要在 for 循环中使用 for,但我不太确定如何构建它。

这是我的代码:

{%- capture list_of_scores -%}
{{wa}}|Wine Advocate,{{bh}}|Burghound,{{ag}}|Vinous,{{jr}}|Jancis Robinson,{{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}{{wa}},{{bh}},{{ag}},{{jr}},{{jg}}{%- endcapture -%}

{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}


{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% endif %}
{% endfor %}

{% for score_and_name in scores_array %}
{% assign split_score_and_name = score_and_name | split: '|' %}
{% assign score = split_score_and_name[0] %}
{% assign score = highest_score %}
{% assign name = split_score_and_name[1] %}
{% endfor %}

<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>

谢谢

你可以这样做

{% assign wa = 12 %}
{% assign bh = 16 %}
{% assign ag = 26 %}
{% assign jr = 6 %}
{% assign jg = 11 %}

{%- capture list_of_scores -%}
    {{wa}}|Wine Advocate,
    {{bh}}|Burghound,
    {{ag}}|Vinous,
    {{jr}}|Jancis Robinson,
    {{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}
    {{wa}},
    {{bh}},
    {{ag}},
    {{jr}},
    {{jg}}
{%- endcapture -%}

{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}

{% assign name = '' %}
{% for score_val in scores %}
    {% assign cur_score = score_val | plus: 0 %}
    {% if cur_score >= highest_score %}
        {% assign highest_score = score_val | plus: 0 %}
        {% assign name = scores_array[forloop.index0] | split: '|' | last %}
    {% endif %}
{% endfor %}

<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>

主要区别在于我们将 name 值作为空变量移出循环,并在循环内检查我们使用 forloop.index0 分配名称变量的最大数字以下代码:

{% assign name = scores_array[forloop.index0] | split: '|' | last %}

所以我们只需要 1 个循环。