Structured Data and Collection Choices
Structured data organizes related values into records and collections. Choose an indexed collection for positional order, a keyed collection for lookup or membership, and a serialization format when values must cross a text boundary.
Choose a list to preserve position, a map to find by identity, and a set to remember membership. A JSON string is a representation of data, not another live collection.
Follow one article through the system
The same article has different representations at different boundaries. An API sends JSON text. Parsing creates an in-memory value. Validation establishes the fields the application accepts. An array keeps display order, a Map supports lookup by ID, and a Set tracks distinct saved IDs. These containers answer different questions.
[{"id":"b","title":"Night"}, …]Parsing is only the first check. Valid JSON can be a number, null, an array of strings, or a record with the wrong fields. The example rejects a malformed article record before building its index. An article ID is an application identifier; the Map does not prove that two records with the same ID have identical fields.
The array preserves [b, a] for display. Looking up a in the Map returns its current record without choosing display order by alphabet. Repeated saved IDs collapse in the Set. If Map.set encounters the same key again, its value is replaced; it does not become an extra record under that key. Decide whether duplicate IDs are legal before silently accepting replacement.
Keys, positions and identity
A plain object's property keys are strings or symbols. A Map can use objects as keys and compares those keys by identity. Converting every key to a string can merge distinct inputs. Use an ordinary record when its named fields are part of a known schema; use a Map when associations themselves are the changing collection. The language does not promise a universal constant-time implementation for every collection operation, so measure a real workload before making absolute performance claims.
Use WeakMap or WeakSet for weak associations to eligible keys, not for a visible list you must enumerate. Their absence of enumeration is deliberate. A Map or Set is not automatically serialized by JSON.stringify into its entries: choose an explicit representation such as [...map] and document the receiving contract.
Repair the boundary
Change one fixture title into a number. JSON.parse still succeeds, but validation rejects it. Then duplicate an ID. Decide whether to keep the first, keep the last, or report an error; write that policy down before implementing it. The independent task uses a fixed policy: return distinct IDs in first-seen order for valid article records supplied by the grader. It does not ask you to silently repair malformed API data.