Im trying to dynamically update jQuery UI source. I can do this fine with an array such as:
var arrProducts = ['cheese' , 'bread' , 'milk'];
But need to do it using an object. Before switching to using AJAX, this was working fine on first page load, passing in an array of objects from PHP into Twig:
var arrProducts = [
{% for product in allproducts %}
{
title: "{{ product.title }}",
url: "{{ product.url }}",
label: "{{ product.label }}"
},
{% endfor %}
];
So , how can I replicate this formatting within javascript? I've tried this:
var arrProducts = [];
$.each(data.products, function(index, product)
{
prod['title'] = product.title;
prod['url'] = product.url;
prod['label'] = product.label;
arrProducts.push(prod);
});
$('.searchBox' ).autocomplete( "option", "source", arrProducts );
But that produces nested objects , which then autocomplete seems not to be able to read properly .