如何用对象过滤对象数组并创建新对象?
How to Filter array of object with object and create new object?
我有一个对象数组
const myData = [
{
"paiteint1": [
{
"status": "healthy",
"date": "2022-05-01",
"hour": "12AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "1AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "2AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "3AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "4AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "5AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "6AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "7AM"
},
],
"paiteint2": [
{
"status": "healthy",
"date": "2022-05-01",
"hour": "12AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "1AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "2AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "3AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "4AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "5AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "6AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "7AM"
},
]
}
]
并且需要根据他的健康状况将json以上的obj转化为以下的甲酸
需要对象
const manipulatedData = [
{
"paiteint1 heathy": {
//all healthy status obj
},
"paiteint1 need attention": {
//all need attention status obj
},
"paiteint1 critical":
{
//all critical status obj
},
"paiteint2 heathy": {
//all healthy status obj
},
"paiteint2 need attention": {
//all need attention status obj
},
"paiteint2 critical":
{
//all critical status obj
},
}
]
我已经尝试在 obj 过滤器 reduce 上进行映射,但无法获得所需的 o/p。
我也在寻找任何其他方法,如果有人可以建议这对我来说会很好。
const myData = [
{
paiteint1: [
{
status: "healthy",
date: "2022-05-01",
hour: "12AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "1AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "2AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "3AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "4AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "5AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "6AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "7AM",
},
],
paiteint2: [
{
status: "healthy",
date: "2022-05-01",
hour: "12AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "1AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "2AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "3AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "4AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "5AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "6AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "7AM",
},
],
},
];
const manipulatedData = [];
myData.forEach((d) => {
Object.keys(d).map((p) => {
return d[p].map((s) => {
manipulatedData[`${p} ${s.status}`] = d[p].filter(
(sp) => sp.status === s.status
);
});
});
});
console.log(manipulatedData);
结果
[
'paiteint1 healthy': [
{ status: 'healthy', date: '2022-05-01', hour: '12AM' },
{ status: 'healthy', date: '2022-05-01', hour: '1AM' },
{ status: 'healthy', date: '2022-05-01', hour: '3AM' },
{ status: 'healthy', date: '2022-05-01', hour: '6AM' }
],
'paiteint1 need attention': [
{ status: 'need attention', date: '2022-05-01', hour: '2AM' },
{ status: 'need attention', date: '2022-05-01', hour: '4AM' }
],
'paiteint1 critical': [
{ status: 'critical', date: '2022-05-01', hour: '5AM' },
{ status: 'critical', date: '2022-05-01', hour: '7AM' }
],
'paiteint2 healthy': [
{ status: 'healthy', date: '2022-05-01', hour: '12AM' },
{ status: 'healthy', date: '2022-05-01', hour: '1AM' },
{ status: 'healthy', date: '2022-05-01', hour: '3AM' },
{ status: 'healthy', date: '2022-05-01', hour: '6AM' }
],
'paiteint2 need attention': [
{ status: 'need attention', date: '2022-05-01', hour: '2AM' },
{ status: 'need attention', date: '2022-05-01', hour: '4AM' }
],
'paiteint2 critical': [
{ status: 'critical', date: '2022-05-01', hour: '5AM' },
{ status: 'critical', date: '2022-05-01', hour: '7AM' }
]
]
const myData = [
{
paiteint1: [
{
status: 'healthy',
date: '2022-05-01',
hour: '12AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '1AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '2AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '3AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '4AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '5AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '6AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '7AM',
},
],
paiteint2: [
{
status: 'healthy',
date: '2022-05-01',
hour: '12AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '1AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '2AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '3AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '4AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '5AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '6AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '7AM',
},
],
},
]
function convertData(data) {
const result = [];
data.forEach((element) => {
const obj = {};
const keys = Object.keys(element);
keys.forEach((key) => {
const status = element[key].map((item) => {
return item.status;
});
const uniqueStatus = [...new Set(status)];
uniqueStatus.forEach((status) => {
obj[key + ' ' + status] = element[key].filter((item) => {
return item.status === status;
});
});
});
result.push(obj);
});
return result;
}
console.log(JSON.stringify(convertData(myData), null, 2));
我有一个对象数组
const myData = [
{
"paiteint1": [
{
"status": "healthy",
"date": "2022-05-01",
"hour": "12AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "1AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "2AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "3AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "4AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "5AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "6AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "7AM"
},
],
"paiteint2": [
{
"status": "healthy",
"date": "2022-05-01",
"hour": "12AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "1AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "2AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "3AM"
},
{
"status": "need attention",
"date": "2022-05-01",
"hour": "4AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "5AM"
},
{
"status": "healthy",
"date": "2022-05-01",
"hour": "6AM"
},
{
"status": "critical",
"date": "2022-05-01",
"hour": "7AM"
},
]
}
]
并且需要根据他的健康状况将json以上的obj转化为以下的甲酸
需要对象
const manipulatedData = [
{
"paiteint1 heathy": {
//all healthy status obj
},
"paiteint1 need attention": {
//all need attention status obj
},
"paiteint1 critical":
{
//all critical status obj
},
"paiteint2 heathy": {
//all healthy status obj
},
"paiteint2 need attention": {
//all need attention status obj
},
"paiteint2 critical":
{
//all critical status obj
},
}
]
我已经尝试在 obj 过滤器 reduce 上进行映射,但无法获得所需的 o/p。 我也在寻找任何其他方法,如果有人可以建议这对我来说会很好。
const myData = [
{
paiteint1: [
{
status: "healthy",
date: "2022-05-01",
hour: "12AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "1AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "2AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "3AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "4AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "5AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "6AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "7AM",
},
],
paiteint2: [
{
status: "healthy",
date: "2022-05-01",
hour: "12AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "1AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "2AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "3AM",
},
{
status: "need attention",
date: "2022-05-01",
hour: "4AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "5AM",
},
{
status: "healthy",
date: "2022-05-01",
hour: "6AM",
},
{
status: "critical",
date: "2022-05-01",
hour: "7AM",
},
],
},
];
const manipulatedData = [];
myData.forEach((d) => {
Object.keys(d).map((p) => {
return d[p].map((s) => {
manipulatedData[`${p} ${s.status}`] = d[p].filter(
(sp) => sp.status === s.status
);
});
});
});
console.log(manipulatedData);
结果
[
'paiteint1 healthy': [
{ status: 'healthy', date: '2022-05-01', hour: '12AM' },
{ status: 'healthy', date: '2022-05-01', hour: '1AM' },
{ status: 'healthy', date: '2022-05-01', hour: '3AM' },
{ status: 'healthy', date: '2022-05-01', hour: '6AM' }
],
'paiteint1 need attention': [
{ status: 'need attention', date: '2022-05-01', hour: '2AM' },
{ status: 'need attention', date: '2022-05-01', hour: '4AM' }
],
'paiteint1 critical': [
{ status: 'critical', date: '2022-05-01', hour: '5AM' },
{ status: 'critical', date: '2022-05-01', hour: '7AM' }
],
'paiteint2 healthy': [
{ status: 'healthy', date: '2022-05-01', hour: '12AM' },
{ status: 'healthy', date: '2022-05-01', hour: '1AM' },
{ status: 'healthy', date: '2022-05-01', hour: '3AM' },
{ status: 'healthy', date: '2022-05-01', hour: '6AM' }
],
'paiteint2 need attention': [
{ status: 'need attention', date: '2022-05-01', hour: '2AM' },
{ status: 'need attention', date: '2022-05-01', hour: '4AM' }
],
'paiteint2 critical': [
{ status: 'critical', date: '2022-05-01', hour: '5AM' },
{ status: 'critical', date: '2022-05-01', hour: '7AM' }
]
]
const myData = [
{
paiteint1: [
{
status: 'healthy',
date: '2022-05-01',
hour: '12AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '1AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '2AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '3AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '4AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '5AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '6AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '7AM',
},
],
paiteint2: [
{
status: 'healthy',
date: '2022-05-01',
hour: '12AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '1AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '2AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '3AM',
},
{
status: 'need attention',
date: '2022-05-01',
hour: '4AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '5AM',
},
{
status: 'healthy',
date: '2022-05-01',
hour: '6AM',
},
{
status: 'critical',
date: '2022-05-01',
hour: '7AM',
},
],
},
]
function convertData(data) {
const result = [];
data.forEach((element) => {
const obj = {};
const keys = Object.keys(element);
keys.forEach((key) => {
const status = element[key].map((item) => {
return item.status;
});
const uniqueStatus = [...new Set(status)];
uniqueStatus.forEach((status) => {
obj[key + ' ' + status] = element[key].filter((item) => {
return item.status === status;
});
});
});
result.push(obj);
});
return result;
}
console.log(JSON.stringify(convertData(myData), null, 2));