comp.lang.javascript FAQ -
http://jibbering.com/faq
Joakim Braun said:
"R. Rajesh Jeba Anbiah" <
[email protected]> skrev i meddelandet
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.