1

I am having a dictionary which maps question-id to the question. While printing contents of dictionary, I want to access the elements in the order in which they were added. The question id is a random 10-digit string. This printing will be done numerous times. A dictionary elements can also be deleted in which case it will simply disappear while leaving the other elements in the exact same order.

It is probably not possible without creating a dictionary class of my own. Or is it?

var question_dic = {};
question_dic["2186242050"] = "Answer to life, universe and everything?";   //adding a key-value pair
delete question_dic[delete_key];   //remove delete_key key
sudeepdino008
  • 2,808
  • 3
  • 31
  • 65
  • 2
    Assuming you're talking about a normal JavaScript object, the properties of it [don't have any guaranteed order](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). If you're not talking about a normal JavaScript object, please describe your dictionary. – James Thorpe Dec 11 '15 at 09:52
  • Where's your code? Also, do do this you can insert one extra element along with the question which is actually the order of insertion and then use a custom comparator to do this. Atleast this approach can be adopted in java – bholagabbar Dec 11 '15 at 09:55
  • @T.J.Crowder Is that ES6/2015? I've not used it too much yet - good to know. Either way, OP needs to clarify the question. – James Thorpe Dec 11 '15 at 09:57
  • This might help: https://github.com/bmcmahen/ordered-dictionary – bholagabbar Dec 11 '15 at 09:57
  • 1
    Is there anything that speaks against using a plain old array? – Yoshi Dec 11 '15 at 10:08
  • @JamesThorpe: Yes. So many people relied on the order that most engines implemented (if we ignore all-digits strings like the OP's, and the handling of prototype properties) that it finally ended up being necessary to specify it. It's covered [here](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys) and [here](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ordinary-object-internal-methods-and-internal-slots-enumerate). I still wouldn't actually *use* it, though. :-) – T.J. Crowder Dec 11 '15 at 10:11
  • @T.J.Crowder: …but that failed. `for … in` and `Object.keys` order is still left unspecified in ES6. – Bergi Dec 11 '15 at 10:12
  • 1
    @T.J.Crowder: https://esdiscuss.org/topic/property-ordering-of-enumerate-getownpropertynames. Admittedly [the wording is very confusing](http://stackoverflow.com/a/30919039/1048572) – Bergi Dec 11 '15 at 10:16
  • 1
    @Bergi: Oh my effing.... Well, it doesn't get more definitive than a quote from Allen Wirfs-Brock from after the specification was finalized. **Wow** that specification language is *massively misleading*. – T.J. Crowder Dec 11 '15 at 10:19
  • @JamesThorpe: They have order now as of ES2015, but only for some operations, such as `getOwnPropertyKeys`. They still have no defined order for `for-in` or `Object.keys`. [Details here](http://stackoverflow.com/q/30076219/1048572). (Thank you Bergi, and no-thank-you massively-misleading specification language!) – T.J. Crowder Dec 11 '15 at 10:45
  • @T.J.Crowder Wonderful! How's that ES2016 coming along... – James Thorpe Dec 11 '15 at 10:47
  • @T.J.Crowder My point being, does _that_ define the order for other operations :) – James Thorpe Dec 11 '15 at 10:50
  • @JamesThorpe Doh! Sorry, misread the digit. I don't see any current proposal for it on the [TC39 ECMA262 github page](https://github.com/tc39/ecma262). – T.J. Crowder Dec 11 '15 at 10:54
  • @T.J.Crowder Just going through the esdiscuss.org link above, he mentions _"As a step in that direction, we should consider for ES7 adding to 9.1.11 something like:"_ so it's at least being _thought_ about – James Thorpe Dec 11 '15 at 10:55

1 Answers1

2

It is probably not possible without creating a dictionary class of my own.

Yes, that's true. You cannot (and should not) use objects like that.

However, this is exactly the use case that Maps are introduced for in ES6. And even if you are not running your scripts in an ES6 environment, you don't need to create such a dictionary class of your own, as there exists a variety of polyfills.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • I am using dictionary along with an array to keep track of the key order. Although the delete operation(involving indexOf and splice) will be expensive. It doesn't matter much, the number of keys will not exceed 30. – sudeepdino008 Dec 11 '15 at 10:29
  • Ah ok, 30 elements is *nothing* :-) – Bergi Dec 11 '15 at 10:32