如何循环数组并将数据存储在液体变量中?

问题描述 投票:0回答:1

我正在使用 JS 和液体执行以下操作:

const variantsWithInventoryData = {};

// Loop through each variant and assign to the object
{% for variant in product.variants %}
  variantsWithInventoryData["{{ variant.id }}"] = {
    inventory_quantity: {{ variant.inventory_quantity }},
    inventory_policy: "{{ variant.inventory_policy }}",
    inventory_management: "{{ variant.inventory_management }}",
    inventory_item_id: 0
  };
{% endfor %}

如何完全使用液体来实现上述目标?

谢谢!

shopify liquid
1个回答
0
投票

{% for variant in variants %}
        {% assign id = variant.id %}
        {% assign size = variant. quantity %} // remove space after "." 
        {% assign color = variant. color %} // remove space after "."
        {% assign res = res | append: id %}
        {% assign res = res | append: " (" %}
        {% assign res = res | append: size %}
        {% assign res = res | append: ", " %}
        {% assign res = res | append: color %}
        {% assign res = res | append: "); " %}

{% endfor %}

{{ res }}
© www.soinside.com 2019 - 2024. All rights reserved.