Dev Helpers
← Back

Build a compound shelf index

Build a compound shelf index. Start with typed records and inspect the inferred result.

Open this example in the interactive library

Source code

type Row = { sku: string; bin: string; qty: number };
const rows: Row[] = [
  { sku: "pen", bin: "A", qty: 4 },
  { sku: "pad", bin: "B", qty: 0 },
  { sku: "pen", bin: "B", qty: 3 },
  { sku: "clip", bin: "A", qty: 9 },
];
const result = Object.fromEntries(
  rows.map((r) => [JSON.stringify([r.bin, r.sku]), r.qty]),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "[\"A\",\"pen\"]": 4,
  "[\"B\",\"pad\"]": 0,
  "[\"B\",\"pen\"]": 3,
  "[\"A\",\"clip\"]": 9
}