我有一个问题,我找不到解决的方法。我有一堆按属性分组的数组:
[
[
{Id: '211321', SendFrom: 'Customer 1', Price: 120, Amount: 2},
{Id: '211341', SendFrom: 'Customer 1', Price: 320, Amount: 5},
{Id: '212351', SendFrom: 'Customer 1', Price: 300, Amount: 2},
{Id: '234121', SendFrom: 'Customer 1', Price: 230, Amount: 3},
{Id: '223321', SendFrom: 'Customer 1', Price: 410, Amount: 1}
],
[
{Id: '2321', SendFrom: 'Customer 2', Price: 120, Amount: 2},
{Id: '21341', SendFrom: 'Customer 2', Price: 320, Amount: 5},
{Id: '2351', SendFrom: 'Customer 2', Price: 300, Amount: 2},
{Id: '4121', SendFrom: 'Customer 2', Price: 230, Amount: 3},
{Id: '3321', SendFrom: 'Customer 2', Price: 410, Amount: 1}
],
[
{Id: '3453', SendFrom: 'Customer 3', Price: 520, Amount: 2},
{Id: '4334', SendFrom: 'Customer 3', Price: 220, Amount: 5},
{Id: '2343', SendFrom: 'Customer 3', Price: 700, Amount: 2},
{Id: '6654', SendFrom: 'Customer 3', Price: 430, Amount: 3},
{Id: '4534', SendFrom: 'Customer 3', Price: 210, Amount: 1}
]
]
在此示例中,对象按SendFrom属性分组。
我想要做的是创建一个这样的对象:
[
{SendFrom: 'Customer 1', Price: 1380, Amount: 13},
{SendFrom: 'Customer 2', Price: 1380, Amount: 13},
{SendFrom: 'Customer 3', Price: 2080, Amount: 13}
]
新对象保留原始的SendFrom值,但包含更新的Price和Amount属性的值。
如何使用JavaScript或lodash获得此功能?
最好的问候,Americo
发布于 2020-06-24 04:18:18
您可以将一个对象用于分组,并将所有需要的值添加到它们的属性中。最后,只获取对象中的值。
var data = [[{ Id: '211321', SendFrom: 'Customer 1', Price: 120, Amount: 2 }, { Id: '211341', SendFrom: 'Customer 1', Price: 320, Amount: 5 }, { Id: '212351', SendFrom: 'Customer 1', Price: 300, Amount: 2 }, { Id: '234121', SendFrom: 'Customer 1', Price: 230, Amount: 3 }, { Id: '223321', SendFrom: 'Customer 1', Price: 410, Amount: 1 }], [{ Id: '2321', SendFrom: 'Customer 2', Price: 120, Amount: 2 }, { Id: '21341', SendFrom: 'Customer 2', Price: 320, Amount: 5 }, { Id: '2351', SendFrom: 'Customer 2', Price: 300, Amount: 2 }, { Id: '4121', SendFrom: 'Customer 2', Price: 230, Amount: 3 }, { Id: '3321', SendFrom: 'Customer 2', Price: 410, Amount: 1 }], [{ Id: '3453', SendFrom: 'Customer 3', Price: 520, Amount: 2 }, { Id: '4334', SendFrom: 'Customer 3', Price: 220, Amount: 5 }, { Id: '2343', SendFrom: 'Customer 3', Price: 700, Amount: 2 }, { Id: '6654', SendFrom: 'Customer 3', Price: 430, Amount: 3 }, { Id: '4534', SendFrom: 'Customer 3', Price: 210, Amount: 1 }]],
grouped = Object.values(data.flat().reduce((r, { SendFrom, Price, Amount }) => {
r[SendFrom] = r[SendFrom] || { SendFrom, Price: 0, Amount: 0 };
r[SendFrom].Price += Price;
r[SendFrom].Amount += Amount;
return r;
}, {}));
console.log(grouped);.as-console-wrapper { max-height: 100% !important; top: 0; }
发布于 2020-06-24 04:20:29
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
console.log(groupBy(your_array_name, 'SendFrom'));发布于 2020-06-24 04:12:24
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
console.log(groupBy(['one', 'two', 'three'], 'length'));
// => {3: ["one", "two"], 5: ["three"]}https://stackoverflow.com/questions/62542931
复制相似问题