Array slice and split in format


I can’t figure out split and join arrays… In twig file I want to split and join a set of array in a format like “xxx”, “yyy”, with quotes with comma. This array is ingredients of a recipe microdata. Every array comes with comma but in a single line with quotes. Like this: “xxx, yyy, zzz,” which I want to make it [ “xxx”, “yyy”, “zzz” ]

Is there a way to make every array with quotes and end with comma?

{% set foo = page.header.ingredients|join(’, ') %}
“recipeIngredient”: “{{ foo }}”,

|join() will stick array items together with some characters. However, it appears your already getting a string from your microdata, just comma separated.

I think you will need to |split() your string into array items, then loop over the items:

{% set ingredients = "xxx, yyy, zzz" %}
{% for ingredient in ingredients|split(',') %}
"{{- ingredient|trim -}}", 
{% endfor %}

Something like that should output:

"xxx", "yyy", "zzz",

Thanks @rhuk you rock :love_you_gesture::love_you_gesture: