Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

need to manage a list in jQuery

Posted on 2011-09-22
7
Medium Priority
?
363 Views
Last Modified: 2012-05-12
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
Comment
Question by:newbieweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36581310
well for a list you would normally use an array.
var uniqueNumber = new Array();
uniqueNumber[0] = number1;
uniqueNumber[1] = number2;
and so on.
0
 

Author Comment

by:newbieweb
ID: 36581322
how do I query the list to see if number1 is found?
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36581340
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:newbieweb
ID: 36581349
thanks.
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 800 total points
ID: 36581351
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
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 1200 total points
ID: 36581634
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
 

Author Closing Comment

by:newbieweb
ID: 36593862
Thanks!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question