Start here.
An array remembers order and permits duplicates. An object maps property names to values. Map and Set are additional collection types with different promises. A Map stores key-value pairs, and its keys can be objects as well as primitives. A Set stores values with uniqueness: adding the same value again does not create another entry. These choices describe the question your data needs to answer.
For a Map, map.set(key, value) stores a pair, map.get(key) retrieves it, and map.has(key) checks whether it exists. get returns undefined when a key is missing, which means has is useful if undefined is also a valid stored value. Map preserves insertion order when iterated. For a Set, add inserts a value, has checks membership, and delete removes it. Its size property reports how many unique values remain.
Uniqueness uses value equality for primitives and identity for objects. Two separately created { id: 1 } objects are distinct Set entries because they are two objects. Choose a stable primitive key, such as an id string, when you want two records with the same id to count as one. Arrays remain best for an ordered list you may index; Map and Set help when lookup or membership expresses the central idea.
Build the model.
A Map stores key-value pairs and accepts keys of any type. A Set stores unique values. Both preserve insertion order during iteration. They make intent visible when the problem is lookup by key or membership rather than a fixed record shape.
Object keys are strings or symbols, and an object also has prototype behavior. Use an object for a known set of named fields; use Map when keys are data. Set compares objects by identity, so two freshly created objects with equal fields are still two members.
How it works.
An object literal is excellent for a fixed record such as {name, age}. When keys are data rather than field names, Map is more direct. map.set(key, value) stores a pair; map.get(key) retrieves it; map.has(key) distinguishes a missing key from a stored undefined value. Keys may be objects, numbers, or strings, and iteration follows insertion order.
Set answers a different question: whether a value is present. set.add(value) does nothing if an equal value is already present. For primitive strings or numbers this usually matches your expectation of uniqueness. For objects, identity matters, so two separate {id: 1} objects remain two entries. If uniqueness should be based on id, store the ids in a Set or define your own lookup policy.
Read it line by line.
const seen = new Set();
seen.add({ id: 1 });
seen.add({ id: 1 });
console.log(seen.size); // 2First notice. Each object literal creates a new identity.
seen begins empty. The first add creates an object and puts that identity in the Set. The second add creates a new object with the same field, but it is a different identity, so the Set keeps it too. seen.size is therefore 2; Set did not inspect the id properties.
See the rule again.
const users = [{ id: 1 }, { id: 1 }, { id: 2 }];
const ids = new Set(users.map(user => user.id));
console.log(ids.size);The result is 2. map extracts the number id from each object; Set sees the repeated primitive number 1 and stores it once. The three user objects still exist. This is uniqueness by id, not object identity.
One thing to remember.
Choose a collection by the relationship it represents.