Dev Helpers
← Back

Get distinct typed ranks

Get distinct typed ranks using TypeScript.

Open this example in the interactive library

Source code

type Row = { id: number; name: string; rank: number | null };
const rows: Row[] = [
  { id: 2, name: "Beta", rank: null },
  { id: 1, name: "Alpha", rank: 3 },
];
console.log([
  ...new Set(
    rows.map((row) => row.rank).filter((rank): rank is number => rank !== null),
  ),
]);

Sample output

[ 3 ]