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.'