stepping threw an array

A

Abby Lee

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');
 
L

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);
}
 
L

Lasse Reichstein Nielsen

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;
// something with trvtp.
}
---
and
---
for (var i in trvtps) {
var trvtp = trvtps;
// 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
 
M

Mick White

Abby 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');
t=trvtps.length;
while(t--){myProcessFunction(trvtps[t])}
Mick
 
E

Evertjan.

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++])
 
E

Evertjan.

Mick White wrote on 09 sep 2004 in comp.lang.javascript:
Evertjan. said:
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 ()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top