Kingsley Simon
Kingsley Simon

Reputation: 2210

Compare two arrays get unique values javascript

So I have an object array called Banker and i have an array called remove_banker_id,

so in my code remove_banker_id = [11, 99]. My banker object banker.id has 11 and 99 and i dont want to include them in my third array so how do i do tht?

My current code has this in my javascript file

   $.each(data, function( index, banker ) {
      $.each(lender_banker_id_array, function(index, banker_id) {
        if(parseInt(banker_id) !== banker.id) {
           banker_object
             .append($('<option>', {value: banker.id, html: banker.name }))
             .removeAttr('disabled');
         }
      })
   });

So basically if any lender_banker_id_array is in banker object, do not append it. But with this code it is not working properly. How do i solve this

Upvotes: 0

Views: 227

Answers (1)

Nathan
Nathan

Reputation: 1090

Try using the jquery utility function $.inArray()

http://api.jquery.com/jquery.inarray/

It returns the index of a value in an array. It returns -1 if the array does not contain the value.

$.each(data, function( index, banker ) {
    if($.inArray(banker.id, lender_banker_id_array) == -1) {
        banker_object.append($('<option>', {
           value: banker.id, 
           html: banker.name 
        })).removeAttr('disabled');
    }
});

Upvotes: 1

Related Questions