0

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;
}
4
  • What's the value of presentationId? Commented Mar 4, 2013 at 13:02
  • it will be 2 or 3 depending on another form. Commented Mar 4, 2013 at 13:03
  • 2
    Seems like the check could just be: if ($.inArray(presentationId, window.list) === -1) { window.list.push(presentationId) } ... This way you don't need to care what presentationId could be in the future (better for maintenance). Commented Mar 4, 2013 at 13:06
  • 1
    @Cory That would make more sense. If presentationId is 2, you don't want to add it twice just because 3 isn't in the array. Though without knowing how this code is being called it's difficult to know what the problem they're experiencing is being caused by; if $.inArray(2, window.list) == -1 is false it should check for the presence of 3 as well. Commented Mar 4, 2013 at 13:08

2 Answers 2

2

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

Comments

1

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];

11 Comments

Hey, great thanks. How can I turn the method call into an if statement?
$(window.list).Contains([0, 1, 2, 3 ], function(foundValue){ console.log("item found"); })
Also how is missingValue called in your JSfiddle? I don't see it mentioned anywhere else in the code.
The code is calling success(item); for the item found in the original array and failure(item); for the item not present in the original array.
How could I convert the function call to an if statement to perform an action based on if no numbers match the array?
|

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.