StackOverflow StackOverflow - 2 months ago 7x
jQuery Question

how to check for dupes & not display them

I want to check for duplicates in the list IDs in this function & not display them, All this is doing is adding the listIDs into the container but it is not checking for duplicates

var listIDs = [];
$.each(this.$el('lists', true), function(index, el) {

Any help would be greatful


To check whether an item already exists in an array, you can use indexOf.

var listIDs = [];
$.each(this.$el('lists', true), function(index, el) {
    var id = $(el).data('containerid')
    if (listIDs.indexOf(id) === -1) {

If id is not already in listIDs, then listIDs.indexOf(id) will return -1. In this case, we add it.