Get unique results from JSON array using jQuery

I don’t know how your products array is built up (so my example might need some modification according to your needs), but you could write a little piece of code that will first collect your unique categories into a new array:

var categories = [];

$.each(catalog.products, function(index, value) {
    if ($.inArray(value.category, categories) === -1) {
        categories.push(value.category);
    }
});

jsFiddle Demo

categories will hold the unique categories you need, you can use it to build your HTML from that (be careful with the IDs of those li elements though, remember that IDs cannot be duplicate, you might give them a little prefix).

Leave a Comment