简体   繁体   中英

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

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');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM