Skip to curriculum
Jump to the editor

21.10. Merge records for the same user

Medium
Topics
20–35 min

The same user often arrives in pieces. A sign-up form gives a name and an email, a profile service adds an address, a billing export adds a phone number and another tag, and a CSV import repeats half of it with blanks. To show one row per person, the pieces have to be grouped by id and merged: later information wins, nested details are combined, lists are joined without repeats, and a blank never erases something already known.

Write mergeUsers(records) in merge-users.js. It returns one merged user per id, in the order each id first appears.

Signature

JAVASCRIPT
function mergeUsers(records) // returns an array of users
  • records: an array of plain objects, each with an id and any other fields. None is changed.
  • Returns a new array with one new object per distinct id.

Requirements

  1. Records with the same id become one user; different ids stay separate. Users are listed in the order their id first appears.
  2. Ids are compared exactly, as Map keys: 1 and "1" are different users. 0 and "" are valid ids.
  3. A record that is not a plain object, or whose id is undefined or null, throws a TypeError.
  4. Records are merged in order. For the same key, a later value replaces the earlier one.
  5. When both values are plain objects, they are merged by these same rules, at every depth.
  6. When both values are arrays, the later array's items are appended unless the merged array already includes them. Items are compared as includes compares them: NaN matches NaN, 1 does not match "1", and objects match only themselves: the same object used in two records gives one item (a copy), while two different objects with the same content give two.
  7. A later null or undefined never replaces a value the merged user already has for that key. For a key the user does not have yet, it is stored.
  8. 0, "" and false are real values and replace earlier ones.
  9. When the two values are of different kinds (an object and a string, an array and an object, null and an object), the later value replaces the earlier.
  10. Dates and other objects that are not plain objects or arrays are taken as they are, never merged.
  11. The records are not changed, and the users share no plain object or array with them.
  12. A key named __proto__, at any depth, is ignored: it never changes a prototype or reaches Object.prototype.

Examples

JAVASCRIPT
mergeUsers([
  { id: 7, name: "Ada", tags: ["math"], address: { city: "London" } },
  { id: 3, name: "Grace" },
  { id: 7, name: null, tags: ["math", "code"], address: { zip: "N1" } },
]);
// [
//   { id: 7, name: "Ada", tags: ["math", "code"], address: { city: "London", zip: "N1" } },
//   { id: 3, name: "Grace" },
// ]
JAVASCRIPT
mergeUsers([{ id: 1, count: 4 }, { id: 1, count: 0 }]); // [{ id: 1, count: 0 }]
mergeUsers([{ id: 1 }, { id: "1" }]);                   // two users
mergeUsers([{ name: "no id" }]);                        // throws TypeError

Environment

  • Plain JavaScript in a browser page; the tests call the global mergeUsers from merge-users.js.
  • Records are JSON-like: no cycles. Only own, enumerable string keys are merged (Object.keys).

Hints

  1. Which built-in collection groups by a key without converting it to a string, and remembers the order keys were added?
  2. If every merged user is a new object that you built, which objects is it safe to change while merging?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…