我希望将一个可以在Shopify主题设置中编辑的字符串,转换为我的Liquid文件中对象键值的数组。
例如:
var text = 'key1:value1,key2:value2,anotherKey:anotherValue'
in to:
var array = [{key1: value1}, {key2: value2}, {anotherKey: anotherValue}]
每个对象在字符串中用逗号隔开,键位于':'左侧,值位于右侧。我需要将其写入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 array = array | concat: key | append: ':' | concat: value %}
{% endfor %}
{{ array }}