The products to de-duplicate.
A new array with later duplicates of each product identity removed.
dedupeProducts([
{ supplier: "Carolina Chemical", cacheKey: "6981" },
{ supplier: "Carolina Chemical", cacheKey: "6981" },
]).length; // 1
export function dedupeProducts(products: readonly Product[]): Product[] {
const seen = new Set<string>();
const result: Product[] = [];
for (const product of products) {
const key = getProductDedupeKey(product);
if (key !== undefined) {
if (seen.has(key)) continue;
seen.add(key);
}
result.push(product);
}
return result;
}
Remove duplicate products from a result set, keeping the first occurrence of each identity as determined by getProductDedupeKey. Products with no usable identity are always kept (never merged with each other). Order is preserved.