Open this example in the interactive library
Source code
type Row = { doc: string; rev: number; author: string };
const rows: Row[] = [
{ doc: "guide", rev: 1, author: "Ada" },
{ doc: "api", rev: 1, author: "Leo" },
{ doc: "guide", rev: 3, author: "Kim" },
{ doc: "guide", rev: 4, author: "Ada" },
];
const result = Object.fromEntries(
rows.toSorted((a, b) => a.rev - b.rev).map((r) => [r.doc, r]),
);
console.log(JSON.stringify(result, null, 2));
Sample output
{
"guide": {
"doc": "guide",
"rev": 4,
"author": "Ada"
},
"api": {
"doc": "api",
"rev": 1,
"author": "Leo"
}
}