Dev HelpersOpen library
← Back

Deduplicate records by ID while keeping the latest

Deduplicate records by ID while keeping the latest in JavaScript.

Open this example in the interactive library

Source code

const rows = [
  { id: 1, version: 1 },
  { id: 2, version: 1 },
  { id: 1, version: 2 },
];
console.log([...new Map(rows.map((row) => [row.id, row])).values()]);

Sample output

[ { id: 1, version: 2 }, { id: 2, version: 1 } ]