小编Bil*_*own的帖子

如何在 Liquid 中将字符串转换为具有键和值的对象数组

我希望将您可以在 Shopify 主题设置中编辑的字符串转换为 Liquid 文件中的对象键值数组。

例如:

var text = 'key1:value1,key2:value2,anotherKey:anotherValue'

in to:

var array = [{key1: value1}, {key2: value2}, {anotherKey: anotherValue}]
Run Code Online (Sandbox Code Playgroud)

每个对象将在字符串中用“,”分隔,键将位于“:”的左侧,值位于右侧。

我需要将其写入文件中theme.liquid,但不确定如何实现这一点。非常感激任何的帮助。

到目前为止我只做到了:

{% assign text = 'key1:value1,key2:value2,anotherKey:anotherValue' %}

{% assign splitText = text | split: ',' %}

{% assign array = '' | split: '' %}
{% for data in splitText %}
    {% assign key = data | split: ':' | first %}
    {% assign value = data | split: ':' | last %}
    {% assign …
Run Code Online (Sandbox Code Playgroud)

liquid shopify

3
推荐指数
1
解决办法
9129
查看次数

标签 统计

liquid ×1

shopify ×1