Jquery add id to element if not found in array

Hello

Using Jquery I am trying to check if a value “x” exists in my array.
Basically, when clicking the “testButton” I have a var “x” that starts from zero Using this value I am trying to check if in the array there is an element with value “0”. If there is, increment “x” and perform the array scan again. This should happen until “x” has a value which is not found in the array. Then at this point I know that I can use this id for the element.
At the moment I have this code that checks if value x is in array, but this happens only for the length of the array.
Next iteration should be: x=3, x is not in array so can be used as id.
Can someone help to implement this. Thanks

  var array = ["0","1","2"];
  var x = 0;

  $("#testButton").click(function(){
        $.each(array, function(index,value){
            if($.inArray(x.toString(),array == -1)){
              console.log('found item with x value in array, increment x and scan the array again');
            }
            else{
              console.log('not in array, add id to element and push current x value to array.');
            }
            x=x+1;
        });
      });

Is there a special reason you have to do it this way? And is the initial array always the one you provided? If it is, why can’t you just find the max value in the array, increment it and use that for the next id?

The array is not always the same. Could also be array = [“2”,“1”,“0”,“7”]. I am doing it like this so that if the user deletes and element from the page with id 1, then the next element that would be added on the page will take 1.
This is only so that I don’t end up with high values for id’s, so I am only using the ones that are available checking the array. I am sure there is a way to do this, I just can’t work my head around it. I guess I would have to remove the foreach, which actually just iterates trough the items of the array, and do it in another way.

So after you have found a suitable new id number, where is the code that pushes that number to the array?

That part I have it already working, with array.push(x) I am just interested in this post to get the basic of my code working properly for checking if x is in array.

Have you looked into this function (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) by any chance? Sorry if I’m not providing you the actual code for your question, just some suggestions that might work.

Looking over the web and searching for a function to use in J query, I came across the inarray which seemed popular and efficient when searching for an element in the array. The function returns the index if the item is found, otherwise returns -1

Instead of looping over each element in the array, you would be better served just with an index value that you increment,

var index = 0;
while (...) { // index is not found in the array
    index += 1;
}
// index is now a unique value

I’ll leave you to supply the code for the while condition.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.