function calculateAssembledSetsAndReturnSkus(suitComponents, inventory) { let componentCount = {}; let minComponent = {}; let result = {}; // Count components in suitComponents Object.entries(suitComponents).forEach(([_, components]) => { Object.entries(components).forEach(([sku, quantity]) => { componentCount[sku] = (componentCount[sku] || 0) + quantity; }); }); // Calculate the minimum sets and find limiting components let minSets = Infinity; for (let sku in componentCount) { let requiredQuantity = componentCount[sku]; let availableQuantity = inventory[sku] || 0; if (availableQuantity < requiredQuantity) { minSets = 0; minComponent[sku] = availableQuantity; } let setsForComponent = Math.floor(availableQuantity / requiredQuantity); minSets = Math.min(minSets, setsForComponent); } // Sort the inventory based on quantity let sortedInventory = Object.entries(inventory).sort((a, b) => a[1] - b[1]); // ... rest of the function remains unchanged
result['sets'] = minSets;
result['leastAvailable'] = getFirstNKeys(minComponent, 3);
result['mostAvailable'] = getLastNKeys(sortedInventory, 3);
result['medianAvailable'] = getMedianNKeys(sortedInventory, 3);
return result;
} // Example usage let suitComponents = { "spu1": { "sku-erp-code1": 2, "sku-erp-code2": 3 }, "spu1-spu2": { "sku-erp-code3": 1, "sku-erp-code4": 2 }, "spu3-spu4": { "sku-erp-code5": 1, "sku-erp-code4": 11 }, "spu3": { "sku-erp-code9": 13, "sku-erp-code2": 21 } }; let inventory = { "sku-erp-code1": 20, "sku-erp-code2": 30, "sku-erp-code3": 15, "sku-erp-code4": 25, "sku-erp-code5": 10, "sku-erp-code9": 50 }; let result = calculateAssembledSetsAndReturnSkus(suitComponents, inventory); console.log(result);
标签:sku,erp,componentCount,customised,202312142321,let,result,data,inventory From: https://www.cnblogs.com/beesky520/p/17902466.html