【问题标题】:How do I flatten an array of arrays in shopify liquid?如何在 shopify 液体中展平一系列阵列?
【发布时间】:2021-06-15 11:00:36
【问题描述】:

我尝试循环遍历数组并使用以下内容捕获输出:

数组:

["cat1","cat2","cat3","cat4"] ["cat1","cat2"] ["cat4"] ["cat5"]

我的尝试:

{% capture newCategories %}
  {% for categoryArr in categories %}
    {% for category in categoryArr %}
      {{category}}
    {% endfor %}
  {% endfor %}
{% endcapture %}

newCategories --> // new array with items

它仍然没有给我一个扁平的数组,有什么想法吗?

【问题讨论】:

  • 需要粘贴实际代码,直到
  • @Onkar 我试过的代码在帖子里

标签: arrays shopify liquid


【解决方案1】:

您应该能够利用 concat 运算符:https://shopify.github.io/liquid/filters/concat/

类似:

{% assign newCategories = '' | split '' %}

{% for categoryArr in categories %}
    {% assign newCategories = newCategories | concat: categoryArr %}
{% endfor %}

【讨论】:

  • 你能添加完整的代码吗?我试图解决这个问题,最终数组没有变化。它似乎没有扩展那些内部数组
  • 老实说,这应该是完整的代码。也许您的categories 数组的结构存在一些问题?也许它没有被存储为数组或类似的东西?
猜你喜欢
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2018-06-12
相关资源
最近更新 更多