Object as Hash

ref – https://medium.com/front-end-hacking/es6-map-vs-object-what-and-when-b80621932373

Object is the great choice for scenarios when we only need simple structure to store data and knew that all the keys are either strings or integers (or Symbol).

An Object in Javascript is dictionary type of data collection — which means it also follows key-value stored concept.

Construction

In general, like in Array, do not use built-in constructor over literal in creating new object, because:

  • More typing
  • Slower performance
  • Confusion & increasing more chances for mistake

for example:

Setting key/values

You can initialize it with key/value pairs, or simply assign it via brackets.
Each key in Object — or we normally call it “property” — is also unique and associated with a single value.
In addition, Object in Javascript has built-in prototype. And don’t forget, nearly all objects in Javascript are instances of Object.

Object: keys must be in simple types (int, string, or symbol)

In Object, it follows the rule of normal dictionary. The keys MUST be simple types — either integer or string or symbols. As shown previously, strings and integers are used often. However, we can also use symbols.

Looping through the Object keys

Looping through symbols

Notice Object.keys and Object.getOwnPropertySymbols

ref – https://stackoverflow.com/questions/34449045/what-is-the-difference-between-reflect-ownkeysobj-and-object-keysobj

Object.keys() returns an array of strings, which are the object’s own enumerable properties.

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

for example: