惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
I
InfoQ
D
Docker
F
Fortinet All Blogs
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
月光博客
月光博客
B
Blog
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
罗磊的独立博客
博客园_首页
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
IT之家
IT之家
V
V2EX

博客园 - Coca-code

202608051132_《theta & freguency measure distance _algorithmn》 202603171903_《实时OS Kernel》 202603162259_《CSS格子(1-9 js版)》 202603162216_《CSS格子(1-9)》 202503271546_《Idiot js code of map app》 202503241513_《Root entrance function of 'Hello Mars' 》 202403172356_《Initial sys. of C》 202401221657_《React相关(一) 写法》 202401102331 -《Flex9格布局——从1到9》 202311141210——《一些修改表字段的sql语句》 202310061823_《运行这个页面,背后是一大堆配置》 202310061227-《心得:低版本mysql配置一,些轮子插件》 202310032035_《近期撸码心得》 202310031029——《个人在Lombok使用中遇到的错误及解决》 202309301820_《Spring boot项目,继承mybatis-generator遇到的问题及解决》 202309272035-《maven依赖已下载,但还是报红,解决办法》 202309272022-《idea编辑器,maven解析依赖慢,解决办法》 202306112142-《最近开发心得...》 202306062001-《远程Linux服务器——安装tomcat8、jdk1.8、mysql5——mysql 用sql建表时提示utf8错误....》 202305281631-《远程Linux服务器——安装tomcat8、jdk1.8、mysql5——mysql workerbench连接出错》
202312142321_《遍历 for customised data structure 》
Coca-code · 2023-12-14 · via 博客园 - Coca-code
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);