Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
{% set var_name1 = "hello" %} {% set var_name2 = "there" %} {% array1|merge({var_name1: var_name2}) %}
我希望上面的代码将它添加到array1:
hello:there
...但它补充说:
var_name1:there
我尝试将 {{ }} 包裹在 var_name1 周围。是否可以将记录添加到数组并使用变量作为键?
将密钥名称括在括号中:
{% array1|merge({(var_name1): var_name2}) %}
请注意,如果 var_name1 是数值,它将不起作用。您必须将其与字符串值连接:
{% set array1 = array1|merge({(var_name1~'_'): var_name2}) %}