471,066 Members | 921 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

for in loop. reverse traverse

Unfortunately, I couldn't find any way to traverse the object array in
reverse order. I'd thought there must be a way to do it with for..in
loop, but couldn't find anything yet. Could someone please help me?
TIA.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 23 '05 #1
3 3208
On 9 Dec 2004 11:17:15 -0800, R. Rajesh Jeba Anbiah
<ng**********@rediffmail.com> wrote:
Unfortunately, I couldn't find any way to traverse the object array in
reverse order.
What's an object array?
I'd thought there must be a way to do it with for..in loop, but couldn't
find anything yet.


The for..in statement doesn't return values in any particular order so
reversing is not possible.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> skrev i meddelandet
news:11**********************@z14g2000cwz.googlegr oups.com...
Unfortunately, I couldn't find any way to traverse the object array in
reverse order. I'd thought there must be a way to do it with for..in
loop, but couldn't find anything yet. Could someone please help me?
TIA.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


A probably meaningless hack would be to do a for...in with the object and
save the property names in an array. Then traverse that array backwards and
access the named properties of the first object.

Joakim Braun
Jul 23 '05 #3
comp.lang.javascript FAQ - http://jibbering.com/faq
"Joakim Braun" <jo**********@jfbraun.removethis.com> wrote in message
news:e7*****************@nntpserver.swip.net...
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> skrev i meddelandet news:11**********************@z14g2000cwz.googlegr oups.com...
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in
loop, but couldn't find anything yet. Could someone please help me?
TIA.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
A probably meaningless hack would be to do a for...in with the object

and save the property names in an array. Then traverse that array backwards and access the named properties of the first object.


But the values retrieved with for...in aren't guaranteed to be in any
particular order in the first place, so "reverse order" has no meaning,
since you can't be sure what the "forward order" will be.

If you want to store key/value pairs and be guaranteed of their order,
you will need to store both the key/value pair and the order they are
added in:

function MyOrderedMap() {
var keys = [];
var values = [];

this.add = function(key, value) {
// test to make sure 'key' isn't in 'keys'
var ii = keys.length;
while (ii-- > 0) {
if (key == keys[ii]) {
return false;
}
}
keys.push(key);
values.push(values);
return true;
}
this.getKeysInReverseOrder = function() {
return keys.reverse();
// or return keys.reverse().join(',');
// or whatever you want
}
}

You probably want methods for getting a value when passed a key:

this.getValue = function(key) {
var ii = keys.length;
while (ii-- > 0) {
if (key == keys[ii]) {
return values[ii];
}
}
return null;
}

and other methods as well to clear the list, etc.

I threw this together in a hurry, there may be syntax errors.

--
Grant Wagner <gw*****@agricoreunited.com>
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
3 posts views Thread by minguskhan | last post: by
22 posts views Thread by delraydog | last post: by
5 posts views Thread by bbgun | last post: by
10 posts views Thread by fig000 | last post: by
15 posts views Thread by Alex Snast | last post: by
reply views Thread by leo001 | last post: by

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.