[英]How to combine 2 Json objects into a single Json object using NodeJS?
I have 3 JSON objects as defined below:我有 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" }];
I have a function which uses array reduce
method:我有一个使用array reduce
方法的 function:
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;
}, {}));
}
Now, I am calling the two functions:现在,我正在调用这两个函数:
const Res1 = createArray(totalData);
const Res2 = createArray(otherTotalData);
const Res3 = createArray(newTotalData);
Here Res1 has the output:这里 Res1 有 output:
[
{
"Hostname": "abc123",
"CName-AStatus": "PASS",
"CName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"CName-CStatus": "FAIL",
"CName-DStatus": "FAIL"
}
]
And, Res2 has the output:而且,Res2 有 output:
[
{
"Hostname": "abc123",
"QName-AStatus": "PASS",
"QName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"QName-CStatus": "FAIL",
"QName-DStatus": "FAIL"
}
]
And, Res3 has the output:而且,Res3 有 output:
[
{
"Hostname": "abc123",
"TName-AStatus": "PASS",
"TName-BStatus": "FAIL"
},
{
"Hostname": "abc235",
"TName-CStatus": "FAIL",
"TName-DStatus": "FAIL"
}
]
I want to combine Res1, Res2 and Res3 such that the final Json object looks as following:我想组合 Res1、Res2 和 Res3,使最终的 Json object 如下所示:
[
{
"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"
}
]
How can I combine Res1
Res2
and Res3
?如何结合Res1
Res2
和Res3
?
Basically you can search the other array with shared property: "Hostname" and use Object.assign
to combine two objects together.基本上,您可以使用共享属性搜索另一个数组:“主机名”并使用Object.assign
将两个对象组合在一起。
const res1 = [{ "Hostname": "abc123", "CName-AStatus": "PASS", "CName-BStatus": "FAIL" }, { "Hostname": "abc235", "CName-CStatus": "FAIL", "CName-DStatus": "FAIL" } ]; const res2 = [{ "Hostname": "abc123", "QName-AStatus": "PASS", "QName-BStatus": "FAIL" }, { "Hostname": "abc235", "QName-CStatus": "FAIL", "QName-DStatus": "FAIL" } ]; const res3 = [{ "Hostname": "abc123", "TName-AStatus": "PASS", "TName-BStatus": "FAIL" }, { "Hostname": "abc235", "TName-CStatus": "FAIL", "TName-DStatus": "FAIL" } ]; function mergeAll(key, ...args) { const arr = Array.from(args); const first = arr[0]; const rest = arr.slice(1); return first.map(r => Object.assign({}, r, ...rest.map(q => q.find(t => t[key] === r[key])))); } const result = mergeAll('Hostname', res1, res2, res3); console.log(result);
Edit: As Op mentioned in the comments there are 3 results to merge so I made a generalized method for any number of results.编辑:正如评论中提到的 Op 有 3 个要合并的结果,所以我为任意数量的结果制定了一个通用方法。
You can just map over one array and use index to get respective value from another array and merge values您可以只在一个数组上使用 map 并使用索引从另一个数组中获取相应的值并合并值
let res1 = [{"Hostname": "abc123","CName-AStatus": "PASS","CName-BStatus": "FAIL"},{"Hostname": "abc235","CName-CStatus": "FAIL","CName-DStatus": "FAIL"}] let res2 = [{"Hostname": "abc123","QName-AStatus": "PASS","QName-BStatus": "FAIL"},{"Hostname": "abc235","QName-CStatus": "FAIL","QName-DStatus": "FAIL"}] let final = res1.map((inp, index) => { return {...inp, ...res2[index] } }) console.log(final)
PS:- This considers the arrays are sorted in same order and always a respective value is present in other array. PS:- 这认为 arrays 以相同的顺序排序,并且始终在其他数组中存在相应的值。 ( considering your given example data ). (考虑到您给定的示例数据)。 If that is not the case then we just need to find the respective value from other array based on some property and the merge.如果不是这种情况,那么我们只需要根据某些属性和合并从其他数组中找到相应的值。
let res1 = [{"Hostname": "abc123","CName-AStatus": "PASS","CName-BStatus": "FAIL"},{"Hostname": "abc235","CName-CStatus": "FAIL","CName-DStatus": "FAIL"}] let res2 = [{"Hostname": "abc235","QName-CStatus": "FAIL","QName-DStatus": "FAIL"},{"Hostname": "abc123","QName-AStatus": "PASS","QName-BStatus": "FAIL"}] let final = [...res1,...res2].reduce((op,inp)=>{ let key = inp.Hostname if(op.has(key)){ op.set(key, {...op.get(key), ...inp} ) } else{ op.set(key, inp) } return op }, new Map()) console.log([...final.values()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.