An exception report can contain the number 7 and the text identifier "7". Grouping records through object property names can merge those keys. A warehouse adapter should decide the identifier contract before it builds a grouped view.
For a Shopify 3PL integration or a drop shipping exception export, preserving the supplied identifier type can be useful while a mapping problem is investigated. It is not a substitute for eventually agreeing the API's identifier format.
Use Map when key identity matters
The helper below accepts a dense array of plain exception records. It groups by a nonempty string or a safe integer identifier, preserving first-seen group order and input order within each group. Each group gets a separate array.
function groupExceptions(rows) {
if (!Array.isArray(rows)) throw new TypeError('array required');
const groups = new Map();
for (let i = 0; i < rows.length; i++) {
if (!Object.hasOwn(rows, i)) throw new TypeError('sparse input');
const row = rows[i];
if (row === null || typeof row !== 'object' || Array.isArray(row)) {
throw new TypeError('record required');
}
if (!Object.hasOwn(row, 'orderId')) throw new TypeError('own orderId required');
const id = row.orderId;
const valid = (typeof id === 'string' && id.length > 0) ||
(typeof id === 'number' && Number.isSafeInteger(id));
if (!valid) throw new TypeError('invalid orderId');
if (!groups.has(id)) groups.set(id, []);
groups.get(id).push(row);
}
return groups;
}
const rows = [
{ orderId: 7, reason: 'address' },
{ orderId: '7', reason: 'stock' },
{ orderId: 7, reason: 'packaging' },
{ orderId: '__proto__', reason: 'reference' },
];
const grouped = groupExceptions(rows);
console.log([...grouped.keys()]);
console.log(grouped.get(7).map(row => row.reason));
The keys are [7, '7', '__proto__'], and the numeric group contains ['address', 'packaging']. The prototype-looking string is an ordinary Map key. MDN describes Map's key equality and insertion order.
State what the helper does not copy
The grouping arrays are new, but their records are the original object references. Editing a grouped record edits the same object held in the input array. Treat the result as a view, or explicitly design a copying step if independent editable records are required.
The exact code was run locally with separate numeric/text IDs, repeated groups, a prototype-looking string, stable ordering, independent group arrays and reference identity. Invalid cases included missing or inherited identifiers, null, empty text, booleans, unsafe numbers and a sparse input array. The helper rejects the whole call; it does not return a partially built report.
For shopify fulfillment, this example is an in-memory data-shaping boundary. It does not reserve stock, send orders or prove a production connection. In china fulfillment reporting, retain the distinction between preserving an incoming value and accepting it as a canonical business reference.
A request named “International Fulfillment for Folligen” would still need an explicit identifier contract before this approach was used. The name is a hypothetical brief, not a customer or integration claim. For a china 3PL handover, YX Fulfillment's store connection checklist gives business context for the identifiers and exception decisions a real integration must agree.
Computer workstations and printing equipment in a warehouse.

Top comments (0)