简体   繁体   English

如何使用 NodeJS 将 2 个 Json 对象组合成一个 Json object?

[英]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 Res2Res3

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.

相关问题 使用 javascript 在 json 中组合对象 - Combine objects in a json using javascript 如何对单个NodeJS / Jade合并使用多个JSON对象 - How to use multiple JSON objects for single NodeJS/Jade Merge 如何使用 NODEJS 和 FileStream 将对象附加/添加到 JSON 对象中的特定对象 - How to append/add objects to a specific object in a JSON object with NODEJS and FileStream 将两个json对象数据合并为一个,将JSON转换为新的Single JSON, - combine two json object data into one ,JSON to new Single JSON, 如何使用NodeJ在JSON对象中获取响应 - How to get response in json object using NodeJs 如何将具有多个对象的JSON对象转换为一组对象 - How to convert JSON object with multiple objects into a single set of objects 将这两个异步释放的json对象合并为单个json字符串 - Combine these 2 json objects released asynchronously into single json string 如何使用 NodeJS 将巨大的 Json 文件读入单个对象? - How do I read a Huge Json file into a single object using NodeJS? 如何只替换我在json数组中没有大量json对象的json数组中json对象的一个​​属性? - How to just replace my one property of json object in json array which have large no of json objects in nodejs? 如何将对象的 object 组合成单个 object Angular - How to combine object of objects in to single object Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM