2 ms·
> I do not understand why they say to use a Map when all keys are the same type and all values are the same type. Also, how would "logic that operates on indivi
by shawnz 12y ago
> I do not understand why they say to use a Map when all keys are the same type and all values are the same type. Also, how would "logic that operates on individual elements" differ between a Map and an Object?
An object is for describing the different properties of one thing. For example:
var myCar = {
make: "Nissan",
model: "Altima",
colour: "Black"
};
See how each value is a different type of data (even though technically they are all strings)? Here, it would make no sense to enumerate over each property because they all have different meanings. Rather, you would use them individually based on what information you needed. On the other hand, a map is for describing a single property for many things:
var manufacturers = new Map([
["Altima", "Nissan"],
["Acura", "Nissan"],
["Accord", "Honda"],
["Camry", "Toyota"]
]);
Here, each key and each value are all the same type of data as one another (models and makes, respectively). Because of that, it would make sense to have the structure be enumerable. Similarly unlike an object, it wouldn't make sense to assume that a particular known key exists, e.g. with hardcoded references to certain keys.