I have some data that I need to retrieve from the Twig PHP template engine and convert into a JS JSON Array.
I am able to loop through the PHP array in TWIG and push the values to JS like so:
var results = []
{% for res in pqRes|reverse %}
var res = "{" + "{{res|raw}}" + "}"
results.push(res)
{% endfor %}
This creates an array like below when I console.log results
0: "{value: 1, meta: 'Meets'}"
1: "{value: 2, meta: 'Exceeds'}"
2: "{value: 1, meta: 'Meets'}"
3: "{value: 1, meta: 'Meets'}"
4: "{value: 0, meta: 'Fails'}"
How can I push these values into a JSON array so that each item doesn't have the quotes around it?
Ultimately what I need to do is push the values into a data structure that looks like this:
var chart = new Chartist.Line('.ct-chart', {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
series: [{
name: 'Workload',
meta: {customData: 123},
data: [
{value: 1, meta: 'This can be anything and will be serialized'},
{value: 4, meta: {text: 'Can even be Objects'}},
{value: 2, meta: 10000},
{value: 1, meta: 'This can be anything and will be serialized'},
{value: 2, meta: 'This can be anything and will be serialized'}
]
}]
}
});