你能帮我解决下面的数据聚合问题吗?
以下是我的初始数组:
const data = [
{ date: '2023-11-01', type: 'A', casualties: 10, state: 'NY', country: 'USA', site: 'hash1' },
{ date: '2023-11-01', type: 'B', casualties: 5, state: 'NY', country: 'USA', site: 'hash2' },
{ date: '2023-11-02', type: 'A', casualties: 15, state: 'NY', country: 'USA', site: 'hash3' },
{ date: '2023-11-01', type: 'C', casualties: 20, state: 'NY', country: 'USA', site: 'hash4' },
{ date: '2023-11-02', type: 'B', casualties: 8, state: 'NY', country: 'USA', site: 'hash5' },
{ date: '2023-11-03', type: 'A', casualties: 25, state: 'NY', country: 'USA', site: 'hash6' },
{ date: '2023-11-01', type: 'D', casualties: 12, state: 'NY', country: 'USA', site: 'hash7' },
];
我想根据casualties
的值聚合类型other
下的部分行.
如果给定的type
在所有日期都有casualties
等于或大于某个阈值,则它应该是results
数组的一部分.
如果给定的type
在所有日期都低于阈值casualties
,但它是唯一低于阈值的type
,则它将成为results
数组的一部分.
如果两个或更多types
在所有日期具有低于特定阈值的casualties
,
它们不应该是results
数组的一部分.对于此方案,
casualties
将 for each 日期求和,type
值应为other
.
otherCasualties
数组应该包含所有日期的casualties
低于阈值的所有types
.
示例:
如果casualtiesThreshold
是9
,则results
数组应为:
results = [
{ date: '2023-11-01', type: 'A', casualties: 10, state: 'NY', country: 'USA' },
{ date: '2023-11-01', type: 'B', casualties: 5, state: 'NY', country: 'USA' },
{ date: '2023-11-02', type: 'A', casualties: 15, state: 'NY', country: 'USA' },
{ date: '2023-11-01', type: 'C', casualties: 20, state: 'NY', country: 'USA' },
{ date: '2023-11-02', type: 'B', casualties: 8, state: 'NY', country: 'USA' },
{ date: '2023-11-03', type: 'A', casualties: 25, state: 'NY', country: 'USA' },
{ date: '2023-11-01', type: 'D', casualties: 12, state: 'NY', country: 'USA' },
];
而otherCasualties
数组应该是[]
.
如果casualtiesThreshold
是13
,则results
数组应为:
results = [
{ date: '2023-11-01', type: 'A', casualties: 10, state: 'NY', country: 'USA'},
{ date: '2023-11-01', type: 'other', casualties: 17, state: 'NY', country: 'USA' },
{ date: '2023-11-02', type: 'A', casualties: 15, state: 'NY', country: 'USA' },
{ date: '2023-11-01', type: 'C', casualties: 20, state: 'NY', country: 'USA' },
{ date: '2023-11-02', type: 'other', casualties: 8, state: 'NY', country: 'USA' },
{ date: '2023-11-03', type: 'A', casualties: 25, state: 'NY', country: 'USA' },
];
而otherCasualties
数组应该是['B', 'D']
.
这就是我try 过的:
function processCasualties(data, casualtiesThreshold) {
const results = [];
const otherCasualties = [];
const groupedData = data.reduce((acc, item) => {
const key = item.type;
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item);
return acc;
}, {});
const types = Object.keys(groupedData);
types.forEach(type => {
const typeData = groupedData[type];
const totalCasualties = typeData.reduce((acc, item) => acc + item.casualties, 0);
const allDatesBelowThreshold = typeData.every(item => item.casualties < casualtiesThreshold);
if (totalCasualties >= casualtiesThreshold || (allDatesBelowThreshold && types.length === 1)) {
results.push(...typeData.map(item => ({
date: item.date,
type: item.type,
casualties: item.casualties,
state: item.state,
country: item.country,
})));
} else if (allDatesBelowThreshold) {
otherCasualties.push(type);
} else {
results.push(...typeData.map(item => ({
date: item.date,
type: 'other',
casualties: totalCasualties,
state: item.state,
country: item.country,
})));
}
});
return { results, otherCasualties };
}
const casualtiesThreshold = 13;
const { results, otherCasualties } = processCasualties(data, casualtiesThreshold);
console.log(results);
console.log(otherCasualties);