blarg
blarg

Reputation: 3893

How to check if different values are not in an array using JQuery inArray?

I have written some code that checks if an array exists and if not, creates one called window.list

It then checks if the array contains a 2 or a 3 value and if not adds them to the list.

if they are within the list already it outputs "Value is already in the list!" then breaks out of the function.

When I execute the code it only recognises the first clause of inArray that checks for a 2 value. 3 does not function. I would like the ability to add more values in the future. I'm not sure this is the most efficient way to write this query.

if (typeof window.list == "undefined" || !(window.list instanceof Array)) {
    window.list = new Array;
}

if ($.inArray(2, window.list) == -1 || $.inArray(3, window.list) == -1) {
    window.list.push(presentationId);
} else {
    console.log("Value is already in the list!")
    return;
}

Upvotes: 0

Views: 539

Answers (2)

Jani Hyytiäinen
Jani Hyytiäinen

Reputation: 5407

You could merge the arrays and leave just the unique values with jQuery

window.list = [2,3];

window.list = $.merge(window.list, [2,3,7,2,60]);
window.list = $.unique(window.list);

//OUTPUT: window.list = [2,3,7,60];

Fiddle Here


OR if you want to check an array against an array and operate on the found / not found instances. You could make a simple plugin Fiddle Here

Your plugin

(function ($) {
  $.fn.Contains = function(value, success, failure) {
      var arr = this;
      if(value instanceof Array){
          $(value).each(function(i,item){
              var found = $.inArray(item, arr) > -1;
              if (found){
                  success(item);
              } else {
                  failure(item);
              }
        });
      }
  };
})(jQuery);

Then call it like:

window.list = [2,3];

$(window.list).Contains([2, 3, 6, 7], function(foundValue){
    // Handle the foundValue here
    alert('found: ' + foundValue);
}, function(missingValue){
    // Handle the missingValue here
    alert('missing: ' + missingValue);
});

Or if you want to use a custom plugin to merge the distinct values to one array with one call, you could do the following Fiddle here:

// Your plugin to merge the distinct values
(function ($) {
  $.fn.MergeDistinct = function(value) {
      var arr = this;
      if(value instanceof Array){
          $(value).each(function(i,item){
              var found = $.inArray(item, arr) > -1;
              if (!found) arr.push(item);
        });
      }
      return arr;
  };
})(jQuery);

// Your list of items
window.list = [2,3];

// Merge the new values without creating duplicates
window.list = $(window.list).MergeDistinct([2, 3, 6, 7]);

//OUTPUT: window.list = [2,3,6,7];

Upvotes: 1

Aravind
Aravind

Reputation: 178

If the presentationID is 2 or 3 why don't you try the code like this:

...
if ($.inArray(presentationID, window.list) == -1) {
    window.list.push(presentationId);
} else {
...

Upvotes: 2

Related Questions