473,499 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stepping threw an array

I've created the following array and want to be able to process each
indivisual object whenever my function is called. Is this best done
with a "foreach"? I don't see a foreach in my javascript book...is
there another way to step threw an array to process each item?

var trvtps = new Array('autoReimb','airRail','car','taxi','lodge',' meals','misc');
Jul 23 '05 #1
6 1206
ab*******@hotmail.com (Abby Lee) writes:
I've created the following array and want to be able to process each
indivisual object whenever my function is called. Is this best done
with a "foreach"? I don't see a foreach in my javascript book...is
there another way to step threw an array to process each item?

var trvtps = new Array('autoReimb','airRail','car','taxi','lodge',' meals','misc');


There are two ways:
---
for (var i = 0; i < trvtps.length; i++) {
var trvtp = trvtps[i];
// something with trvtp.
}
---
and
---
for (var i in trvtps) {
var trvtp = trvtps[i];
// something with trvtp.
}
---

The former is targeted at an array (something with a length and
integer indices) whereas the latter works on any object and
iterates all enumerable properties, not only integer ones.

As long as you haven't added properties to "Object.prototype",
"Array.prototype" or "trvtps" itself, then only the integer properties
are properties of the array.

If you have an array where not all positions have a value, the
for(...in...) will skip these positions, whereas the incrementing
for loop will access all positions less than the length.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
Lee
Abby Lee said:

I've created the following array and want to be able to process each
indivisual object whenever my function is called. Is this best done
with a "foreach"? I don't see a foreach in my javascript book...is
there another way to step threw an array to process each item?

var trvtps = new
Array('autoReimb','airRail','car','taxi','lodge', 'meals','misc');


The length attribute of the array tells you how many entries
it contains. They will be numbered from 0 to length-1.

for(var i=0;i<trvtps.length;i++){
alert(trvtps[i]);
}

Jul 23 '05 #3
Abby Lee wrote:
I've created the following array and want to be able to process each
indivisual object whenever my function is called. Is this best done
with a "foreach"? I don't see a foreach in my javascript book...is
there another way to step threw an array to process each item?

var trvtps = new Array('autoReimb','airRail','car','taxi','lodge',' meals','misc');

t=trvtps.length;
while(t--){myProcessFunction(trvtps[t])}
Mick
Jul 23 '05 #4
Mick White wrote on 09 sep 2004 in comp.lang.javascript:
t=trvtps.length;
while(t--){myProcessFunction(trvtps[t])}


var t=0;
while(t<trvtps.length)
myProcessFunction(trvtps[t++])
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #5
Evertjan. wrote:
Mick White wrote on 09 sep 2004 in comp.lang.javascript:
t=trvtps.length;
while(t--){myProcessFunction(trvtps[t])}
var t=0;
while(t<trvtps.length)
myProcessFunction(trvtps[t++])

Both approaches work equally well. Did you try both?
Mick
Jul 23 '05 #6
Mick White wrote on 09 sep 2004 in comp.lang.javascript:
Evertjan. wrote:
Mick White wrote on 09 sep 2004 in comp.lang.javascript:
t=trvtps.length;
while(t--){myProcessFunction(trvtps[t])}

var t=0;
while(t<trvtps.length)
myProcessFunction(trvtps[t++])

Both approaches work equally well. Did you try both?


Who are you asking, Mick?

I just like to have my pointers increasing and minimizing superfluous ()
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
278
by: c# newbie | last post by:
When stepping through code, to find where an error is thrown, the problem is that I have to step threw the statement that causes the error, and if it's in a class that's instantiated from the main...
3
19835
by: David Lozzi | last post by:
Hello, I'm receving the following on my webserver (Windows 2003 Server SP1) Event ID: 5 Source: Active Server Pages Error: File /KQShopping/ordercomplete4.asp Script Engine Exception. A...
5
368
by: pinaki_m77 | last post by:
Hi, I am trying to debug a C++ program using Microsoft VC++ IDE. The program loads a dynamic link library (dll) and later makes calls to functions inside this dll. I want to step inside the code of...
25
3978
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
0
1137
by: stand__sure | last post by:
Stepping into a stored procedure used to be fairly straight-forward, but after following the guidance in all 6 or so of the MSDN pages about enabling debugging of stored procedures in SQL Server...
6
12976
by: Aero | last post by:
Hi, My window application written in C# is throwing following exception while connecting to one FTP location The type initializer for System.Net.Sockets.Socket threw an exception This exe is...
4
1339
kamill
by: kamill | last post by:
i want to make a website that contains admin section and i needed that at a perticular time only one user can enter threw the admin section.. How can i achive this ( threw php)?
0
1080
by: =?Utf-8?B?TWlrZSBPS0M=?= | last post by:
VB 2003 As I am stepping through code, I want to see a data table in a grid, so I can easily see the data in the data table. Currently I QuickWatch the data table, then I have to add ".rows(0)"...
7
1614
by: Lyn | last post by:
Is there a solution to this problem? While stepping through code (F8) it would sometimes be helpful to observe changes occurring on the affected form in the Access window. However, the current...
0
7007
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
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
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
5470
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,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.