473,241 Members | 1,820 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,241 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 3344
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
8
by: Edwinah63 | last post by:
Hi Everyone, in vb6 i was able to execute the following code and it would close the children is the reverse order they were opened eg the last child opened was the first child to close. in...
3
by: minguskhan | last post by:
Does anyone know how to reverse a string using a loop?
22
by: delraydog | last post by:
It's quite simple to walk to the DOM tree going forward however I can't figure out a nice clean way to walk the DOM tree in reverse. Checking previousSibling is not sufficient as the...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
5
by: bbgun | last post by:
Hello all- I'm trying to write a progam that reads in a single text file, then copies the contents in reverse order to a new file. I'm guessing the most efficient way to do this is to read the...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
15
by: Alex Snast | last post by:
Hello I'm new to python and i can't figure out how to write a reverse for loop in python e.g. the python equivalent to the c++ loop for (i = 10; i >= 0; --i)
7
beacon
by: beacon | last post by:
Hi everybody, This may be an easy one, but I'm having a lot of trouble with it. I have a continuous form and I want to validate that the user has entered something in each of the required fields...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.