Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Javascript
Associative array help needed.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="RobG, post: 4884469"] Douglas Crockford wrote: [...] Yes Douglas - but the rest of the question was how to loop through the array. My guess is that the OP will attempt to call each element explicitly, but what about getting all of them without needing to know what they are called? So I had a go at looping through all the elements of all the objects, but I'm left with two questions (hopefully I've used do/while correctly!): 1. Is there a way to avoid using eval? 2. Is there an efficient way of putting the j decrement inside the while, but still get the zero-th element? I could have used: j = myArray.length; ... eval('myArray[j-1].' + anAttribute) ... while(--j) but (without testing) all those [j-1]'s seem inefficient. <script type="text/javascript"> myArray = [{ name: "John", age: "35", number: "12345", date: "01/01/1975"}, { name: "Sarah", age: "29", number: "56789", date: "12/12/19"}]; function sayArray() { var msg='', anAttribute, j; if (myArray) { j = myArray.length - 1; do { msg += '\nEntry: ' + j; for ( anAttribute in myArray[j]){ msg += '\n ' + anAttribute + ': ' + eval('myArray[j].' + anAttribute); } --j; } while(j >= 0); } alert(msg); } <button onclick="sayArray();">Say myArray</button> [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Javascript
Associative array help needed.
Top