So lets say I have a hash
a = { foo : "bar" , zap : "zip" , baz : "taz" }
I can interate over it like this
for( var n in a ) {
doSomething(a[n]);
}
but I want to go backwards through it…
thoughts on how to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no forwards or backwards with object properties, as there is no ordering defined by the specification. I’m pretty sure that there’s not even a guarantee that two traversals will necessarily give you the property names in the same order.
(Strictly speaking, you also don’t know that the property names are hashed, though that doesn’t matter much.)
If you need to maintain an ordering (perhaps according to the order in which properties have been added), you’d have to wrap up the property storage with something that maintained a parallel array containing the keys. It could then expose iterator APIs to allow traversal forwards and backwards.