Day 5 of 100DaysOfCode

Day 5

Objects and Maps were the order of the day today. Objects are arbitrary collections of properties. They can be created by using braces as an expression. The properties inside the braces are separated by commas. Each property has a name followed by a colon and a value. If a property name is not a valid binding word or number, then it has to be quoted.

let day 5 = {
    coded: true,
    topics: [“objects”, “Maps”],
};

To find out what properties an object has, you can use the Object.keys function. You give it an object, and it returns an array of strings—the object’s property names.

console.log(Object.keys({x: 0, y: 0, z: 2}));

The Object.assign function copies all properties from one object into another.

let objectA = {a: 1, b: 2};
Object.assign(objectA, {b: 3, c: 4});

Objects are MUTABLE, meaning that you can change their properties. A single object value can have different content at different times. With objects, there is a difference between having two references to the same object and having two different objects that contain the same properties.

Maps are objects, with a few important differences: • Maps only contain what is explicitly put in them, meaning that they do not have any default keys. This is not true for objects.

• A Map’s key can be any value, including functions or even other objects. The keys for an Object MUST BE either a string or a Symbol.

• A map object will iterate entries, keys, and values in the order of entry insertion. Even though the keys in an Object are ordered now, this was not always the case, and as such the order is complex.

• The number of items in a Map can be easily retrieved using the size property, while this has to be done manually for an Object.

• You can directly iterate a map while you can’t with an Object. For an object you would need to use Object.keys or Object.entries

• Map works better when there are frequent additions and removals of key-value pairs