gpt4 book ai didi

javascript - 按数组中的多个属性对对象进行分组,然后求和它们的值

转载 作者:可可西里 更新时间:2023-11-01 01:45:51 25 4
gpt4 key购买 nike

Grouping elements in array by multiple properties是最接近我的问题的匹配项,因为它确实按数组中的多个键对对象进行分组。问题是此解决方案不会汇总属性值然后删除重复项,而是将所有重复项嵌套在二维数组中。

预期行为

我有一个对象数组,必须按 shapecolor 分组。

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}
];

只有当它们的 shapecolor 相同时,此数组中的对象才被认为是重复的。如果是,我想分别总结它们的 usedinstances 值,然后删除重复项。

因此在此示例中,结果数组可能仅包含四种组合:square redsquare bluecircle redcircle blue

问题

我在这里尝试了一种更简单的方法:

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: 'red', used: 4, instances: 4},
{shape: 'square', color: 'red', used: 2, instances: 2}
];

result = [];

arr.forEach(function (a) {
if ( !this[a.color] && !this[a.shape] ) {
this[a.color] = { color: a.color, shape: a.shape, used: 0, instances: 0 };
result.push(this[a.color]);
}
this[a.color].used += a.used;
this[a.color].instances += a.instances;
}, Object.create(null));

console.log(result);

但它输出

[{shape: "square", color: "red", used: 11, instances: 9},
{shape: "circle", color: "blue", used: 4, instances: 4}]

而不是预期的结果:

[{shape: "square", color: "red", used: 5, instances: 3},
{shape: "circle", color: "red", used: 2, instances: 1},
{shape: "square", color: "blue", used: 11, instances: 9},
{shape: "circle", color: "blue", used: 0, instances: 0}]

如何让我的函数按形状和颜色正确地对对象进行分组?即总结他们的值(value)并删除重复项?

最佳答案

使用Array#reduce使用辅助对象对相似对象进行分组。对于每个对象,检查组合的 shapecolor 是否存在于助手中。如果没有,请使用 Object#assign 添加到助手中创建对象的副本,并推送到数组。如果是,请将其值添加到 usedinstances

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}];

var helper = {};
var result = arr.reduce(function(r, o) {
var key = o.shape + '-' + o.color;

if(!helper[key]) {
helper[key] = Object.assign({}, o); // create a copy of o
r.push(helper[key]);
} else {
helper[key].used += o.used;
helper[key].instances += o.instances;
}

return r;
}, []);

console.log(result);

如果可以使用 ES6,则使用 Map收集值,然后通过 spreading 将其转换回数组Map#values :

const 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}];

const result = [...arr.reduce((r, o) => {
const key = o.shape + '-' + o.color;

const item = r.get(key) || Object.assign({}, o, {
used: 0,
instances: 0
});

item.used += o.used;
item.instances += o.instances;

return r.set(key, item);
}, new Map).values()];

console.log(result);

关于javascript - 按数组中的多个属性对对象进行分组,然后求和它们的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46794232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com