I discovered something interesting today. Apparently, you can treat an object like an array…or so I thought. Reviewing a colleague’s code showed that you can add key/value entries to an existing object in a manner I would have associated with an array. But, wait…its not an array 🙂
- While simple in concept once you get it, it is a bit confusing when you first review it. Running the sample doesn’t show anything terribly unusual as we see an object with properties. Also not surprising is that we can print the properties out like an array using Object.keys.
- Now, looking at the code, you can see we create an object. It gets weird when you see how the other properties are added (i.e. very array like). Kinda cool!
- Chris Fahnert (credit for this method)