I have this JQuery / Javascript block of code to filter my list by search terms. It’s very long and looks a little unprofessional. Is there a way I could simplify it ?
Like with something “.contains()” ?
If someone could point me in the right direction I’d greatly appreciate it. Thanks !
Heres the code ; Ask me if you need more :
$.each(catalog.products,
function(index, value) {
if ((filterValue == '' || filterValue == null)
|| value.name.toUpperCase().indexOf(filterValue.toUpperCase()) != -1
|| value.brand.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1
|| value.category.toUpperCase().indexOf(filterValue.toUpperCase()) != -1
|| value.sport.toUpperCase().indexOf(filterValue.toUpperCase()) != -1)
{
items.push('<li id="' + index + '">' +
'<a data-identity="productId" href="./details.page?productId=' + index + '" >' +
'<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
'<p>' + value.brand + '</p>' +
'<h3>' + value.name + '</h3>' +
'<span class="ui-li-count">' + value.price + ' $</span></li>') +
'</a>';
}
}
);
A straightforward way to ‘simplify’ the code would be