Data MergingLoading saved progress…

Data Merging

You're given an array of row objects, each tagged with a user field plus one or more other fields. Different rows can describe the same user — one row carries their name, another their age. Your job is to consolidate every row belonging to a user into a single record, so each user appears exactly once in the output. This is a shallow merge per group, the same idea as Object.assign applied to each user's rows in turn.

Signature

// rows: Array<{ user: string } & Record<string, unknown>>
//   each row has a `user` field plus any number of other fields.
// returns: Array<{ user: string } & Record<string, unknown>>
//   one merged record per user, in the order each user was FIRST seen.
function dataMerging(rows): Array<object>;

Examples

// Two rows for 'ada' carry disjoint fields; they merge into one record.
dataMerging([
  { user: 'ada', name: 'Ada' },
  { user: 'ada', age: 36 },
  { user: 'linus', name: 'Linus' },
]);
// → [{ user: 'ada', name: 'Ada', age: 36 }, { user: 'linus', name: 'Linus' }]
// When two rows set the same field, the LATER row wins.
dataMerging([
  { user: 'ada', role: 'guest' },
  { user: 'ada', role: 'admin' },
]);
// → [{ user: 'ada', role: 'admin' }]

Notes

  • Group by user, return an array. Rows with the same user collapse into one record; the output is an array of those records, not an object.
  • Later fields override earlier ones. If two rows of the same user both set role, the value from the row that appears later in the input wins.
  • First-seen order. Users come out in the order they first appear in the input, not sorted.
  • Keep the user field. It stays on every merged record.
  • Empty in, empty out. dataMerging([]) returns [].
  • Don't mutate the input. Build fresh records; leave the original rows untouched.
Loading editor…