ChemPal Documentation - v0.0.13-beta.5
    Preparing search index...

    Function getAllUniqueValues

    • Gets a sorted unique list of values for a column from all rows. Returns all values, including the filtered out ones.

      Type Parameters

      • TData

      Parameters

      • column: Column<TData, unknown>

        The column to get values from

      • table: Table<TData>

        The table instance

      Returns (string | number)[]

      A sorted array of unique values

      export function getAllUniqueValues<TData>(
      column: Column<TData, unknown>,
      table: Table<TData>,
      ): (string | number)[] {
      const accessorKey = (column.columnDef as ColumnDefWithAccessor<TData>).accessorKey;
      if (!accessorKey) return [];

      const uniqueValues = table.options.data.reduce<(string | number)[]>((accu, row) => {
      const value = row[accessorKey as keyof TData] as string | number;
      if (value !== undefined && value !== null && !accu.includes(value)) {
      accu.push(value);
      }
      return accu;
      }, []);

      return uniqueValues.sort((a, b) => {
      if (typeof a === "number" && typeof b === "number") return a - b;
      return String(a).localeCompare(String(b));
      });
      }