0

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

1 Answer 1

1

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');
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

neat, sweet answer @nathan. thnks bruv
I highly recommend using Array.prototype.indexOf() it's faster and also is just better to use a native function against a non native.
@vihan1086 it probably is faster, but it's not supported in IE < 9. So if you want to have broad browser support you'll have to use a polyfill or something and at some point the trade off isn't worth the extra millisecond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.