[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

need to manage a list in jQuery

I need to create a list, fill it with unique 8 digit numbers, then query the list to see if the next 8 digit number is unique. If it's already in the list my function will return true. If it gets through the whole list and does not find a match, I will return false.

What is the syntax to:

1) create the list
2) add an item to the list
3) check the list to see if a given 8 digit number is found.

Thanks.
0
newbieweb
Asked:
newbieweb
  • 3
  • 3
2 Solutions
 
haloexpertsexchangeCommented:
well for a list you would normally use an array.
var uniqueNumber = new Array();
uniqueNumber[0] = number1;
uniqueNumber[1] = number2;
and so on.
0
 
newbiewebSr. Software EngineerAuthor Commented:
how do I query the list to see if number1 is found?
0
 
haloexpertsexchangeCommented:
If you are using jquery as your title suggests then you can use jquery inarray for part of it.
http://api.jquery.com/jQuery.inArray/
Let me put something together to test.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
newbiewebSr. Software EngineerAuthor Commented:
thanks.
0
 
ZvonkoSystems architectCommented:
Check this:
<script>

var myList=[];
for(var i=1;i<9;i++){
  myList['_'+(12345670+i)]=(12345670+i);
}


function checkList(theList,theNum){
  return theList['_'+theNum]!=undefined;
}

alert(checkList(myList,12345678)); // returns: true
alert(checkList(myList,12345687)); // returns: false

</script>

Open in new window

0
 
haloexpertsexchangeCommented:
This works, I just filled in the array with any old numbers, with this array you will get false returned.
var uniqueNumber = new Array();
uniqueNumber[0] = 3082969;
uniqueNumber[1] = 3082963;
uniqueNumber[2] = 3082963;
uniqueNumber[3] = 3082965;
uniqueNumber[4] = 3082966;
uniqueNumber[5] = 3082967;

var checkNumbers = function(uniqueNumber) {

while(uniqueNumber.length!=0){
var searchFor = uniqueNumber[0];
uniqueNumber.shift();
if(jQuery.inArray(searchFor, uniqueNumber)!=-1)
return false;
}
return true;
};

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now