使用液体,是否有一种方法可以过滤数组并忽略非匹配项

发布于 2025-01-29 21:48:32 字数 379 浏览 2 评论 0原文

将液体用于商店。

我想浏览一个值数组,并检查这些值是否与其他值列表匹配。如果它们匹配,我想显示它们。如果它们不匹配,我想忽略它们。

目前,我可以通过查看所有值然后“删除”我不想要单独的值来反向做到这一点,但这是一种可怕的方法。

{% for 'field' in [metafield.key.value] | split: ", " %}
  {% if field == 'value 1' or field == 'value 2' or field == 'value 3'%}
    <div>
     field
    </div>
  {% else %}
    {% continue %}
  {% endif %}

Using Liquid for a store.

I want to look through an array of values and check to see if any of those values match another list of values. If they match i want to display them. if they do not match i want to ignore them.

I'm currently able to do it in reverse by looking at all the values then 'remove' the ones i don't want individually but that is a terrible way to do this.

{% for 'field' in [metafield.key.value] | split: ", " %}
  {% if field == 'value 1' or field == 'value 2' or field == 'value 3'%}
    <div>
     field
    </div>
  {% else %}
    {% continue %}
  {% endif %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2025-02-05 21:48:32

对于未来的疲倦旅行者:

这项工作可能需要调整分裂弦的位置:

{% assign metafield_str = product.metafields.yada.yada | split: "," %}
{% assign control_str = 'value, something, test, test2' | split: "," %}

{%- capture variable -%}
{% for c_str in control_str %}
  {% for m_str in metafield_str %}
    {% if c_str == m_str %}
      {{m_str}}
    {% endif %}
  {% endfor %}
{% endfor %}
{%- endcapture -%}

for future weary travelers:

this works you may need to adjust where you're splitting the string:

{% assign metafield_str = product.metafields.yada.yada | split: "," %}
{% assign control_str = 'value, something, test, test2' | split: "," %}

{%- capture variable -%}
{% for c_str in control_str %}
  {% for m_str in metafield_str %}
    {% if c_str == m_str %}
      {{m_str}}
    {% endif %}
  {% endfor %}
{% endfor %}
{%- endcapture -%}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文