如何将一些数据减少到新的对象中?
更新 我尝试了自己的解决方案,这是我所做的,也许可以做得更好
const dbData = [{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_CALLCENTER',
current: 5,
total: 17,
},
{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
{
studyId: 'Y',
siteId: 'B',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
{
studyId: 'Y',
siteId: 'B',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
{
studyId: 'Y',
siteId: 'B',
day: '2000-01-01',
status: 'PENDING_CALLCENTER',
current: 3,
total: 9,
},
];
const reduced = dbData.reduce((acc, row) => {
const {
studyId,
siteId,
status,
current,
total
} = row;
const idx = acc.findIndex(x => studyId === x.studyId && siteId === x.siteId);
const item = idx === -1 ? {
studyId,
siteId,
currents: {},
totals: {}
} : { ...acc[idx]
};
item.currents[status] = item.currents[status] ? item.currents[status] + current : current;
item.totals[status] = item.totals[status] ? item.totals[status] + total : total;
if (idx === -1) {
acc.push(item);
} else {
acc[idx] = item;
}
return acc;
}, []);
console.log(reduced);
我是使用减少的新手,并且在理解如何正确使用它时遇到了一些困难。
我想解决一个问题,我需要在具有不同形状的新的OBJ中减少一些数据。
我有一些数据,
[
{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_CALLCENTER',
current: 5,
total: 17,
},
{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
];
我想将其简化为新数据,
[
{
studyId: 'X',
siteId: 'A',
currents: {
PENDING_CALLCENTER: 5,
PENDING_SITE: 3,
},
totals: {
PENDING_CALLCENTER: 17,
PENDING_SITE: 9,
},
},
];
以了解我能够创建一个降低器来计算总和,但是我做到了,但是上述问题我很难开始并想理解它。
update
I tried my own solution and this is what I did maybe can be done better
const dbData = [{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_CALLCENTER',
current: 5,
total: 17,
},
{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
{
studyId: 'Y',
siteId: 'B',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
{
studyId: 'Y',
siteId: 'B',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
{
studyId: 'Y',
siteId: 'B',
day: '2000-01-01',
status: 'PENDING_CALLCENTER',
current: 3,
total: 9,
},
];
const reduced = dbData.reduce((acc, row) => {
const {
studyId,
siteId,
status,
current,
total
} = row;
const idx = acc.findIndex(x => studyId === x.studyId && siteId === x.siteId);
const item = idx === -1 ? {
studyId,
siteId,
currents: {},
totals: {}
} : { ...acc[idx]
};
item.currents[status] = item.currents[status] ? item.currents[status] + current : current;
item.totals[status] = item.totals[status] ? item.totals[status] + total : total;
if (idx === -1) {
acc.push(item);
} else {
acc[idx] = item;
}
return acc;
}, []);
console.log(reduced);
I'm new to using reduce and I have some difficulties understanding how to use it correctly.
I would like to solve a problem where I need to reduce some data in a new array of obj with a different shape.
I have some data
[
{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_CALLCENTER',
current: 5,
total: 17,
},
{
studyId: 'X',
siteId: 'A',
day: '2000-01-01',
status: 'PENDING_SITE',
current: 3,
total: 9,
},
];
I want to reduce this to this new data
[
{
studyId: 'X',
siteId: 'A',
currents: {
PENDING_CALLCENTER: 5,
PENDING_SITE: 3,
},
totals: {
PENDING_CALLCENTER: 17,
PENDING_SITE: 9,
},
},
];
To learn I was able to create a reducer which calc sum and that I did it but the above problem I have just difficulties to start and want to understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试这样的事情:
请注意 - 我没有考虑验证/异常处理。这只是使用减少数据的一种方法。您还可以找到一种可爱的减少代码行的方式 - 自由:-)
输出:
You can try something like this :
Please note - I haven't considered validation/exception handling. This is just one way of using a reduce for your data. You could also find a cute way of reducing lines of code - feel free :-)
Output:
以下还原功能仅合并
currents
和totals
同一ID的对象的值。因此,对于y
/b
的重复项,例如OP提供的重复项,相同属性的数值将被覆盖,但不会汇总。The following reducer functionality just merges the
currents
andtotals
values of objects of same IDs. Thus, for duplicate items like provided by the OP with a duplicate forY
/B
, number values for same properties will be overwritten but not summed-up.