我正在寻找一种方法来整合 javascript 中的对象集合。例如我有一个集合:
inventory = [
{count: 1, type: "Apple"},
{count: 2, type: "Orange"},
{count: 1, type: "Berry"},
{count: 2, type: "Orange"},
{count: 3, type: "Berry"}
]
我想要结束的是:
inventory = [
{count: 1, type: "Apple"},
{count: 4, type: "Orange"},
{count: 4, type: "Berry"}
]
有没有一种优雅的方法可以做到这一点,它不涉及获取类型列表、在我的集合中搜索这些类型、对值求和以及用总和创建一个新数组?