Problems › TypeScript › orders
Split an order across warehouses
Fulfilment needs the order regrouped by the warehouse each line ships from, so it can raise one pick list per site.
- Group the SKUs under their warehouse code.
- Within a warehouse, keep the order the lines arrived in.
- A warehouse with no lines does not appear at all.
splitByWarehouse(rows: list<Row>) → map<string, list<string>>
Where you start
function splitByWarehouse(rows: Row[]): Record<string, string[]> {
}
Worked examples
| Call | Result |
|---|---|
splitByWarehouse([{"sku":"A1","warehouse":"IST"},{"sku":"B2","warehouse":"ANK"},{"sku":"C3","warehouse":"IST"}]) | {"IST":["A1","C3"],"ANK":["B2"]} |
splitByWarehouse([{"sku":"A1","warehouse":"IST"}]) | {"IST":["A1"]} |
splitByWarehouse([]) | {} |
splitByWarehouse([{"sku":"X","warehouse":"IZM"},{"sku":"X","warehouse":"IZM"}]) | {"IZM":["X","X"]} |
Hint
A map from code to a growing list. Create the list the first time you meet a code.
Reference solution in TypeScript
function splitByWarehouse(rows: Row[]): Record<string, string[]> {
const result: Record<string, string[]> = {};
for (const r of rows) {
if (!result[r.warehouse]) result[r.warehouse] = [];
result[r.warehouse].push(r.sku);
}
return result;
}