You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript嵌套对象按类别汇总价格与数量并排序求助

JavaScript嵌套对象分类汇总解决方案

需求说明

对嵌套对象objToBeSumUp按category字段的值分类,同时汇总price和quantity的数值,生成包含totalPrice和totalQuantity的新对象,并按totalPrice降序排序。

待汇总对象

const objToBeSumUp = {
    0: {
        value: {
            category: {
                value: 'a' // 注意:原代码中a、b未加引号,实际运行需修正为字符串
            },
            price: {
                value: 500
            },
            quantity: {
                value: 5
            }
        }
    },
    1: {
        value: {
            category: {
                value: 'a'
            },
            price: {
                value: 300
            },
            quantity: {
                value: 3
            }
        }
    },
    2: {
        value: {
            category: {
                value: 'b'
            },
            price: {
                value: 800
            },
            quantity: {
                value: 8
            }
        }
    },
    3: {
        value: {
            category: {
                value: 'b'
            },
            price: {
                value: 400
            },
            quantity: {
                value: 4
            }
        }
    }
};

预期结果

const objExpected = {
    0: {
        value: {
            category: {
                value: 'b'
            },
            totalPrice: {
                value: 1200
            },
            totalQuantity: {
                value: 12
            }
        }
    },
    1: {
        value: {
            category: {
                value: 'a'
            },
            totalPrice: {
                value: 800
            },
            totalQuantity: {
                value: 8
            }
        }
    }
};

半成品代码问题说明

原代码仅能单值汇总,且存在两个关键问题:

  • objToBeSumUp是对象而非数组,直接使用.length会导致遍历失效
  • 仅处理了price的累加,未兼顾quantity,且存储结构不符合预期

完整实现代码

// 1. 按category分组,同时累加price和quantity
const groupedData = Object.values(objToBeSumUp).reduce((acc, currentItem) => {
    const category = currentItem.value.category.value;
    const price = currentItem.value.price.value;
    const quantity = currentItem.value.quantity.value;

    // 若当前分类未初始化,创建对应嵌套结构
    if (!acc[category]) {
        acc[category] = {
            value: {
                category: { value: category },
                totalPrice: { value: 0 },
                totalQuantity: { value: 0 }
            }
        };
    }

    // 同时累加两个字段的值
    acc[category].value.totalPrice.value += price;
    acc[category].value.totalQuantity.value += quantity;

    return acc;
}, {});

// 2. 将分组结果转为数组并按totalPrice降序排序
const sortedArray = Object.values(groupedData).sort((itemA, itemB) => {
    return itemB.value.totalPrice.value - itemA.value.totalPrice.value;
});

// 3. 将排序后的数组转为预期的索引式对象
const objExpected = sortedArray.reduce((acc, item, index) => {
    acc[index] = item;
    return acc;
}, {});

console.log(objExpected);

代码说明

  • 使用Object.values()将原对象转为数组,解决对象无法通过.length遍历的问题
  • 通过reduce方法完成分组与双字段累加,每个分类对应完整的嵌套结构
  • 用sort方法按totalPrice降序排列分组结果
  • 最后再次使用reduce将排序后的数组转为索引式对象,完全匹配预期格式

内容的提问来源于stack exchange,提问作者Hammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 07:07:55