У меня есть 3 JSON объектов, как определено ниже:
const totalData = [{ Hostname: "abc123", name: "CName-A", Status: "PASS", Heading: "Not Applicable" },
{ Hostname: "abc123", name: "CName-B", Status: "FAIL", Heading: "Not Applicable" },
{ Hostname: "abc235", name: "CName-C", Status: "FAIL", Heading: "Not Applicable" },
{ Hostname: "abc235", name: "CName-D", Status: "FAIL", Heading: "Not Applicable" }];
const otherTotalData = [{ Hostname: "abc123", name: "QName-A", Status: "PASS", Heading: "HeadingQA" },
{ Hostname: "abc123", name: "QName-B", Status: "FAIL", Heading: "HeadingQB" },
{ Hostname: "abc235", name: "QName-C", Status: "FAIL", Heading: "Not Applicable" },
{ Hostname: "abc235", name: "QName-D", Status: "FAIL", Heading: "Not Applicable" }];
const newTotalData = [{ Hostname: "abc123", name: "TName-A", Status: "PASS", Heading: "HeadingTA" },
{ Hostname: "abc123", name: "TName-B", Status: "FAIL", Heading: "HeadingTB" },
{ Hostname: "abc235", name: "TName-C", Status: "FAIL", Heading: "Not Applicable" },
{ Hostname: "abc235", name: "TName-D", Status: "FAIL", Heading: "Not Applicable" }];
У меня есть функция, которая использует array reduce
метод:
function createArray(totalData) {
const keyValues = ["Status", "Heading"],
result = Object.values(totalData.reduce((res, { Hostname, name, ...o }) => {
res[Hostname] = res[Hostname] || { Hostname };
keyValues.forEach(k => res[Hostname][name + k] = o[k]);
return res;
}, {}));
}
Теперь я вызываю два функции:
const Res1 = createArray(totalData);
const Res2 = createArray(otherTotalData);
const Res3 = createArray(newTotalData);
Здесь Res1 имеет выход:
[
{
"Hostname": "abc123",
"CName-AStatus": "PASS",
"CName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"CName-CStatus": "FAIL",
"CName-DStatus": "FAIL"
}
]
И, Res2 имеет выход:
[
{
"Hostname": "abc123",
"QName-AStatus": "PASS",
"QName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"QName-CStatus": "FAIL",
"QName-DStatus": "FAIL"
}
]
И, Res3 имеет выход:
[
{
"Hostname": "abc123",
"TName-AStatus": "PASS",
"TName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"TName-CStatus": "FAIL",
"TName-DStatus": "FAIL"
}
]
Я хочу объединить Res1, Res2 и Res3 так, чтобы конечный объект Json выглядел следующим образом:
[
{
"Hostname": "abc123",
"CName-AStatus": "PASS",
"CName-BStatus": "FAIL",
"QName-AStatus": "PASS",
"QName-BStatus": "FAIL",
"TName-AStatus": "PASS",
"TName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"CName-CStatus": "FAIL",
"CName-DStatus": "FAIL",
"QName-CStatus": "FAIL",
"QName-DStatus": "FAIL",
"TName-CStatus": "FAIL",
"TName-DStatus": "FAIL"
}
]
Как я могу объединить Res1
Res2
и Res3