Can you iterate JavaScript object properties in insertion order?

Yes.  Yes, you can.  At least as of ES2015.  I found this out when working with the jstree contextmenu plugin.  That plugin receives an object with named keys for the items to show in the menu.  It assumes that jQuery’s $.each will traverse those items in the same order they were added to the object.  $.each boils down to a for-in loop.  Well, turns out that since ES2015, the for-in loop returns properties in a deterministic order!  As long as your keys are strings that can’t be parsed as integers, you will get them out in the same order they were added.

Useful links:

This relates to my new Google Chrome extension, TabFern, about which more news soon.  If you have lots of browser windows and tabs open, or want to manage multiple browsing sessions, TabFern may be just what you are looking for!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.