相关疑难解决方法(0)

按数组中的多个属性对对象进行分组,然后汇总它们的值

通过多个属性对数组中的元素进行分组是与我的问题最接近的匹配,因为它确实通过数组中的多个键对对象进行分组.问题是这个解决方案没有总结属性值然后删除重复项,而是将所有重复项嵌入二维数组中.

预期的行为

我有一个对象数组,必须按shape和分组color.

var arr = [
    {shape: 'square', color: 'red', used: 1, instances: 1},
    {shape: 'square', color: 'red', used: 2, instances: 1},
    {shape: 'circle', color: 'blue', used: 0, instances: 0},
    {shape: 'square', color: 'blue', used: 4, instances: 4},
    {shape: 'circle', color: 'red', used: 1, instances: 1},
    {shape: 'circle', color: 'red', used: 1, instances: 0},
    {shape: 'square', color: 'blue', used: 4, instances: 5},
    {shape: 'square', color: 'red', used: 2, instances: 1}
];
Run Code Online (Sandbox Code Playgroud)

此数组中的对象仅在它们shape和 …

javascript arrays duplicates

16
推荐指数
6
解决办法
2万
查看次数

标签 统计

arrays ×1

duplicates ×1

javascript ×1