以不同格式处理数据
Manipulating data in a different format
我设法创建了一个查询,使用以下脚本从数据库中获取一些数据:
app.get("/diseases", async (req, res) => {
const rows = await process.postgresql.query(
"SELECT ds.disease, ds.symptom FROM diseases ds"
);
console.log(rows);
res.status(200).send(JSON.stringify(rows));
});
数据采用以下格式:
[
{ disease: ' boală hipertensivă', symptom: ' durere toracică' },
{
disease: ' boală hipertensivă',
symptom: ' respirație întretăiată'
},
{ disease: ' boală hipertensivă', symptom: ' amețeală' },
{ disease: ' boală hipertensivă', symptom: ' astenie' },
{ disease: ' boală hipertensivă', symptom: ' toamnă (Fall)' },
{ disease: ' boală hipertensivă', symptom: ' sincopă' },
{ disease: ' diabet', symptom: ' poliurie' }
]
我的问题是如何将此数据转换为如下格式:
{"Hypertensive disease": ["Pain chest", "Shortness of breath", "Dizziness", "Asthenia", "Fall", "Syncope", "Vertigo", "Sweat", "Sweating increased", "Palpitation", "Nausea", "Angina pectoris", "Pressure chest"], "Diabetes": ["Polyuria"]}
您正在尝试对数据进行分组和转置。最好的方法是在数组上使用 .reduce()
函数。这是一个例子:
const arr = [
{ disease: "boală hipertensivă", symptom: " durere toracică" },
{ disease: "boală hipertensivă", symptom: " respirație întretăiată" },
{ disease: "boală hipertensivă", symptom: " amețeală" },
{ disease: "boală hipertensivă", symptom: " astenie" },
{ disease: "boală hipertensivă", symptom: " toamnă (Fall)" },
{ disease: "boală hipertensivă", symptom: " sincopă" },
{ disease: "diabet", symptom: " poliurie" }
];
console.log(arr.reduce((group, row) => {
// Check if disease group has the disease
if (typeof group[row.disease] === "undefined") {
// Initialise an array.
group[row.disease] = [];
}
// Push the symptoms.
group[row.disease].push(row.symptom.trim());
// return the updated group.
return group;
}, {}));
.as-console-wrapper { max-height: 100% !important; top: 0; }
对于上面的给定输入,我得到这样的响应:
{
"boală hipertensivă": [
"durere toracică",
"respirație întretăiată",
"amețeală",
"astenie",
"toamnă (Fall)",
"sincopă"
],
"diabet": [
"poliurie"
]
}
return rows.reduce((acc, item) => { if (!!acc[item.disease]) acc[item.disease].push(item.symptom); else acc[item.disease] = [item.symptom]; return acc; }, {})
reduce
函数有两个参数——一个迭代处理函数,它累积一个结果,以及初始结果(我们把一个空对象作为初始结果{}
)。
迭代处理器有两个参数 - 累加器对象 acc
和当前项 item
,它应该 return 新的(或修改的)累加器对象。
在每次迭代中,我们的函数都会检查 acc
是否已经有一个具有 'disease' 值的字段。如果没有' - 它会创建此字段并在其中放入 1 个项目数组 [item.symptom]
。如果该字段已经存在,它会将症状推送到该数组。在我们处理数据之后,我们得到以疾病为键的对象,以他们的症状为值的数组。
我设法创建了一个查询,使用以下脚本从数据库中获取一些数据:
app.get("/diseases", async (req, res) => {
const rows = await process.postgresql.query(
"SELECT ds.disease, ds.symptom FROM diseases ds"
);
console.log(rows);
res.status(200).send(JSON.stringify(rows));
});
数据采用以下格式:
[
{ disease: ' boală hipertensivă', symptom: ' durere toracică' },
{
disease: ' boală hipertensivă',
symptom: ' respirație întretăiată'
},
{ disease: ' boală hipertensivă', symptom: ' amețeală' },
{ disease: ' boală hipertensivă', symptom: ' astenie' },
{ disease: ' boală hipertensivă', symptom: ' toamnă (Fall)' },
{ disease: ' boală hipertensivă', symptom: ' sincopă' },
{ disease: ' diabet', symptom: ' poliurie' }
]
我的问题是如何将此数据转换为如下格式:
{"Hypertensive disease": ["Pain chest", "Shortness of breath", "Dizziness", "Asthenia", "Fall", "Syncope", "Vertigo", "Sweat", "Sweating increased", "Palpitation", "Nausea", "Angina pectoris", "Pressure chest"], "Diabetes": ["Polyuria"]}
您正在尝试对数据进行分组和转置。最好的方法是在数组上使用 .reduce()
函数。这是一个例子:
const arr = [
{ disease: "boală hipertensivă", symptom: " durere toracică" },
{ disease: "boală hipertensivă", symptom: " respirație întretăiată" },
{ disease: "boală hipertensivă", symptom: " amețeală" },
{ disease: "boală hipertensivă", symptom: " astenie" },
{ disease: "boală hipertensivă", symptom: " toamnă (Fall)" },
{ disease: "boală hipertensivă", symptom: " sincopă" },
{ disease: "diabet", symptom: " poliurie" }
];
console.log(arr.reduce((group, row) => {
// Check if disease group has the disease
if (typeof group[row.disease] === "undefined") {
// Initialise an array.
group[row.disease] = [];
}
// Push the symptoms.
group[row.disease].push(row.symptom.trim());
// return the updated group.
return group;
}, {}));
.as-console-wrapper { max-height: 100% !important; top: 0; }
对于上面的给定输入,我得到这样的响应:
{
"boală hipertensivă": [
"durere toracică",
"respirație întretăiată",
"amețeală",
"astenie",
"toamnă (Fall)",
"sincopă"
],
"diabet": [
"poliurie"
]
}
return rows.reduce((acc, item) => { if (!!acc[item.disease]) acc[item.disease].push(item.symptom); else acc[item.disease] = [item.symptom]; return acc; }, {})
reduce
函数有两个参数——一个迭代处理函数,它累积一个结果,以及初始结果(我们把一个空对象作为初始结果{}
)。
迭代处理器有两个参数 - 累加器对象 acc
和当前项 item
,它应该 return 新的(或修改的)累加器对象。
在每次迭代中,我们的函数都会检查 acc
是否已经有一个具有 'disease' 值的字段。如果没有' - 它会创建此字段并在其中放入 1 个项目数组 [item.symptom]
。如果该字段已经存在,它会将症状推送到该数组。在我们处理数据之后,我们得到以疾病为键的对象,以他们的症状为值的数组。