Dev Helpers
← Back

Count last-place mentions

Count last-place mentions. Start with typed records and inspect the inferred result.

Open this example in the interactive library

Source code

type Row = { name: string; picks: string[] };
const rows: Row[] = [
  { name: "Ada", picks: ["A", "C", "B"] },
  { name: "Kim", picks: ["C", "A", "B"] },
  { name: "Leo", picks: ["A", "B", "C"] },
];
const result = Object.fromEntries(
  [...new Set(rows.flatMap((r) => r.picks))].map((p) => [
    p,
    rows.filter((r) => r.picks.at(-1) === p).length,
  ]),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "A": 0,
  "C": 1,
  "B": 2
}