gpt4 book ai didi

javascript - 如何在数组中使用reduce方法将具有公共(public)键的多个Json对象转换为单个Json对象?

转载 作者:行者123 更新时间:2023-12-04 10:08:30 24 4
gpt4 key购买 nike

我在 NodeJS 中有一个函数,它返回 Json Javascript 对象。

let rows = function1();

这里的行看起来像:
"rows": [
{
"Hostname": "abc123",
"name": "name1",
"Status": "PASS",
"Heading": "Not Applicable"
},
{
"Hostname": "abc123",
"name": "name2",
"Status": "FAIL",
"Heading": "Not Applicable"
},
{
"Hostname": "abc235",
"name": "name1",
"Status": "FAIL",
"Heading": "Not Applicable"
},
{
"Hostname": "abc235",
"name": "name2",
"Status": "FAIL",
"Heading": "Not Applicable"
}
]

在这里, rows同一个 Hostname 可以有任意数量的 Json 对象 key :
"rows": [
{
"Hostname": "abc123",
"name": "name1",
"Status": "PASS",
"Heading": "Not Applicable"
},
{
"Hostname": "abc123",
"name": "name2",
"Status": "FAIL",
"Heading": "Not Applicable"
},
{
"Hostname": "abc123",
"name": "name3",
"Status": "FAIL",
"Heading": "Not Applicable"
},
...
{
"Hostname": "abc235",
"name": "name1",
"Status": "FAIL",
"Heading": "Not Applicable"
},
{
"Hostname": "abc235",
"name": "name2",
"Status": "FAIL",
"Heading": "Not Applicable"
},
{
"Hostname": "abc235",
"name": "name3",
"Status": "FAIL",
"Heading": "Not Applicable"
}
...
]

我要修改 rows array这样所有 Javascript 对象都属于一个 Hostname被转换为单个 Json 对象:
"rows": [
{
"Hostname": "abc123",
"name1Status": "PASS",
"name1Heading": "Not Applicable"
"name2Status": "FAIL",
"name2Heading": "Not Applicable"
"name3Status": "FAIL",
"name3Heading": "Not Applicable"
...
},
{
"Hostname": "abc235",
"name1Status": "FAIL",
"name1Heading": "Not Applicable"
"name2Status": "FAIL",
"name2Heading": "Not Applicable"
"name3Status": "FAIL",
"name3Heading": "Not Applicable"
...
}
]

我编写了以下代码,但它会覆盖所有键值对并显示与最后一个 Hostname 相关的 Json 对象只要
const res = rows.reduce((acc, rec) => {
let result = { ...acc, [`Hostname`]: rec.Hostname, [`${rec.name}Status`]: rec.Status, [`${rec.name}Heading`]: rec.Heading }
return result
}, [])

{
"Hostname": "abc235",
"name1Status": "FAIL",
"name1Heading": "Not Applicable"
"name2Status": "FAIL",
"name2Heading": "Not Applicable"
"name3Status": "FAIL",
"name3Heading": "Not Applicable"
}

可能有某种形式的 for loop res 之外以便它考虑到所有不同的 Hostname key

最佳答案

您可以使用一个对象进行分组并返回分组值。

var data = [{ Hostname: "abc123", name: "name1", Status: "PASS", Heading: "Not Applicable" }, { Hostname: "abc123", name: "name2", Status: "FAIL", Heading: "Not Applicable" }, { Hostname: "abc235", name: "name1", Status: "FAIL", Heading: "Not Applicable" }, { Hostname: "abc235", name: "name2", Status: "FAIL", Heading: "Not Applicable" }],
keys = ["Status", "Heading"],
result = Object.values(data.reduce((r, { Hostname, name, ...o }) => {
r[Hostname] = r[Hostname] || { Hostname };
keys.forEach(k => r[Hostname][name + k] = o[k]);
return r;
}, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何在数组中使用reduce方法将具有公共(public)键的多个Json对象转换为单个Json对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61452889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com