Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  62] [ 2]  / answers: 1 / hits: 32409  / 14 Years ago, sun, may 9, 2010, 12:00:00

JavaScript objects have no order stored for properties (according to the spec). Firefox seems to preserve the order of definition of properties when using a for...in loop. Is this behaviour something that I can rely on? If not is there a piece of JavaScript code somewhere that implements an ordered hash type?


More From » javascript

 Answers
7

This question come up as the top search result. After not finding a ordered hash, i just wrote this small coffescript. Hopefully this will help folks landing on this page:



## OrderedHash
# f = new OrderedHash
# f.push('a', 1)
# f.keys()
#
class OrderedHash
constructor: ->
@m_keys = []
@m_vals = {}

push: (k,v) ->
if not @m_vals[k]
@m_keys.push k
@m_vals[k] = v

length: () -> return @m_keys.length

keys: () -> return @m_keys

val: (k) -> return @m_vals[k]
vals: () -> return @m_vals

[#96835] Thursday, May 6, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laytonlamontm

Total Points: 745
Total Questions: 130
Total Answers: 130

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
;