blarg
blarg

Reputation: 3893

How to check if variable value is contained within an array Javascript

I have an ifElseif statement that adds elements to a list using jQuery sortable based on presentationID. I would like to prevent the same element being added more than once.

The idea I cam up with is to create an array called 'disregardList' and before the ifElseif statement use a separate if statement to check if the presentationID is already contained within the array, which it wont be on the first time round. Then execute the ifElseif statement and at the end add the current presentationID to the array. Here's what I have so far.

  function presentationAddToAgenda(presentationId)
           {    

              var disregardList = new Array();

                 if (disregardList[i] != presentationId) 
                    {
                       ifElseif statement.......
                       {
                       Blaa Blaa Blaa 

                       disregardList.push("presentationId");

                    }
            };

Upvotes: 0

Views: 145

Answers (1)

Matt Cain
Matt Cain

Reputation: 5768

You mentioned that you are using jQuery, so you could use the jQuery.inArray() function to test if the value is in your disregard list already, like this:

if ($.inArray(presentationId, disregardList) == -1) { // -1 means not found
    // do stuff
}

Upvotes: 3

Related Questions