473,387 Members | 1,420 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,387 software developers and data experts.

Looping through an array

Take a look at this code:

var arr = new Array();
arr["id1"] = 1; // element 0
arr["id5"] = 2; // element 1
arr["id47"] = 3; // element 2

Is it possible to loop through this array without knowing
the string-id's? Just start out at element 0, and go on
to 1 and 2 and print out the values?
--
Jarle Kaste
jk**********@online.no ['Unlock' my address]
Jul 20 '05 #1
2 6586
"Jarle Kaste" <jk**********@online.no> writes:
Take a look at this code:

var arr = new Array();
arr["id1"] = 1; // element 0
arr["id5"] = 2; // element 1
arr["id47"] = 3; // element 2

Is it possible to loop through this array without knowing
the string-id's? Just start out at element 0, and go on
to 1 and 2 and print out the values?


First of all, there is no need to use an array when you don't use
integer indices. You might as well use an object, i.e.
var arr = new Object();

To iterate through the properties of any object, you can use the
for(...in...) construct:
for (var i in arr) {
... arr[i] ...
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
In article <8S*******************@news4.e.nsc.no>,
jk**********@online.no enlightened us with...
Take a look at this code:

var arr = new Array();
arr["id1"] = 1; // element 0
arr["id5"] = 2; // element 1
arr["id47"] = 3; // element 2

Is it possible to loop through this array without knowing
the string-id's? Just start out at element 0, and go on
to 1 and 2 and print out the values?

<body>
hi
<script>
var arr = new Array();
arr["id1"] = 1; // element 0
arr["id5"] = 2; // element 1
arr["id47"] = 3; // element 2
for (v in arr)
alert(arr[v]);

</script>

--
-------------------------------------------------
~kaeli~
There is no justification or rationalization
for mutilation. Ban declawing as inhumane.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #3

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

Similar topics

2
by: ensnare | last post by:
Hi all, I'm using a database session handler and am looking to loop through data residing in the sessions table to make a 'Users online' array. I've found that using urldecode on the data...
2
by: CanoeGuy | last post by:
I have an array that I am populating from a database. I am looping through the array to write the values to a datagrid. At this point, I'm stuck. I can't figure out how to read the first four...
1
by: Nicole | last post by:
Hello! I hope there is someone out there who can shed some light on this for me. I have a module that is supposed to look at an access table, pull out each bid record, link to another table to...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
11
by: Jim Whitaker | last post by:
Expand full for viewing... I have a parent/child table. The basic setup is as follows: The parent table is called load sheet. It has fields such as Tripno, carrier, loaddate, etc. The child...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
1
by: delraydog | last post by:
I have an associative array that I need to loop through, allbills, however, each element in this array requires processing by the user and I need to capture the users actions on the element and...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
4
by: wbosw | last post by:
I'm trying to take a word (stEAdy) and find the index positions of the uppercase characters(2,3) in the word. I have them stored in an array (positionarray). Then I reverse the word and set all...
3
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.