What is the correct way to limit results of ui.autocomplete when used with map?
I found similar issue with solution which works for simple array here: Limit results in jQuery UI Autocomplete But my array is a bit nested, apart from the fact that the source is external.
I have also tested another solution here to undefined error: jquery autocomplete limit results
Below is what I adapted from the link, but to no success.
My test:
var sourceUrl = '/path/to/products.json';
$("#auto").autocomplete({
source: function (request, response) {
$.getJSON(sourceUrl, {term: request.term}, function (result) {
response($.map(result.products, function (item) {
var myarray = item.product;
console.log(myarray); // the returns look expected (Product One, Product Two, etc)
var results = $.ui.autocomplete.filter(myarray, request.term);
results.slice(0, 10);
}));
});
}
});
products.json:
{"products": [{"label": "Product One", "product": "Product One", "sku": "12345"}, ...
It will be easier to simply output all the products, but they are are just to many/heavy to hold in dropdowns. Thats why I need to limit the result.
Any hint is very much appreciated. Thanks.