473,383 Members | 1,813 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,383 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 3350
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.