473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

javascript loop?

how do you make a loop that stops outputting after one item has been output from another loop?
May 11 '07 #1
2 1427
iam_clint
1,208 Expert 1GB
example? cause i don't understand what your asking.
May 11 '07 #2
gits
5,390 Expert Mod 4TB
i think you nested 2 loops for the purpose of comparing them or whatever, so you may have something like this:

Expand|Select|Wrap|Line Numbers
  1. var list1 = [1, 2, 3, 4, 5, 6];
  2. var list2 = ['a', 'b', 'c', 3, 'd', 'e'];
  3.  
  4. for (var i in list1) {
  5.     for (var j in list2) {
  6.         if (list1[i] == list2[j]) {
  7.             alert('found ' + list1[i] + ' in both lists');
  8.     }
  9. }
  10.  
its a good idea to break the entire looping when the inner condition returns true ... so you may do the following:

Expand|Select|Wrap|Line Numbers
  1. var list1 = [1, 2, 3, 4, 5, 6];
  2. var list2 = ['a', 'b', 'c', 3, 'd', 'e'];
  3.  
  4. outerloop: for (var i in list1) {
  5.     for (var j in list2) {
  6.         if (list1[i] == list2[j]) {
  7.             alert('found ' + list1[i] + ' in both lists');
  8.             break outerloop;
  9.     }
  10. }
  11.  
but have in mind ... that this is not the best way to compare ... until you need two loops you may have n*m steps to do. what about n+m? ;) so have a look at the following possibilty:

Expand|Select|Wrap|Line Numbers
  1. var list1 = [1, 2, 3, 4, 5, 6];
  2. var list2 = ['a', 'b', 'c', 3, 'd', 'e'];
  3. var lookup = {};
  4.  
  5. for (var j in list2) {
  6.     lookup[list2[j]] = list2[j];
  7. }
  8.  
  9. for (var i in list1) {
  10.     if (typeof lookup[list1[i]] != 'undefined') {
  11.         alert('found ' + list1[i] + ' in both lists');
  12.         break;
  13.     } 
  14. }
  15.  
hope this helps ... ;)
May 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Fawke101 | last post by:
Hi There, I have a table with 3 rows. 1st has the table/column headers, 2nd contains the column data and the 3rd is where the loop for the SQL statement lies. I also have a JS sort function on...
2
by: bunnyman | last post by:
I have a for each loop in javascript, of which I need to output to an ASP array. unfortunantly not too familiar with javascript.. the loop is for items ordered in a shopping cart. they are...
0
by: Daniel | last post by:
hi, I want to find all the objects in my ascx embeded in another ascx of aspx. How can i use JavaScript loop through all the objects in order to find the name i want. For instance, using...
5
by: nescio | last post by:
hello, i have made an application in php so that people can make, on the fly, a form. when they submit the form there is a javascript formvalidation. because we do not know how many fields...
4
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
6
by: Jim | last post by:
I'm a newbie so I'm probably doing something simple and dumb but I just can't get Blogger to accept a JavaScript for/next loop. Here's what I've tried <Script Language='JavaScript> { for...
0
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.