2

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) {
        listIDs.push($(el).data('containerid'));
    });

Any help would be greatful

  • 1
    If you could add the corresponding HTML, that _would be greatful_. Please see [ask] and **[mcve]** – Tushar Aug 11 '16 at 15:28
  • Does this help: http://stackoverflow.com/questions/10096019/php-array-push-how-not-to-push-if-the-array-already-contains-the-value ? – jamjamg Aug 11 '16 at 15:30
  • Just check if the containerid is already inside the listID array (indexOf) before you push it. – Shilly Aug 11 '16 at 15:31

2 Answers2

1

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) {
        listIDs.push(id)
    }
})

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

Jim O'Brien
  • 2,512
  • 18
  • 29
0

You might try unique(). API here.

Then you could have var uniqueIDs = ListIDs.unique() and just use uniqueIDs for display purposes.

Hope that helps.