[英]How in JS to merge in one object two json objects where the ID of on object correspond on the same ID of the second object
My question relates to the fact I'm querying 2 different objects from DB and the result is in JSON. I need to merge them into one.我的问题与我从数据库中查询 2 个不同的对象有关,结果在 JSON 中。我需要将它们合并为一个。
The 2 objects have in common this two key/value IRBId =...
and id =...
and they look as an example这 2 个对象有共同的这两个键/值
IRBId =...
和id =...
它们看起来像一个例子
OBJ 1对象 1
{
"data":{
"IRBs":{
"nodes":[
{
"id":"8",
"name":"Admin ",
},
{
"id":"9",
"name":"Again",
}
],
}
}
}
OBJ 2对象 2
{
"data":{
"informedConsentForms":{
"count":3,
"nodes":[
{
"id":"93",
...
"IRBId":"9",
},
{
"id":"92",
...
"IRBId":"8",
},
{
"id":"91",
...
"IRBId":"8",
}
],
}
},
As you will see above OBJ 2 and OBJ 1 corresponding with the same at IRBid and id.正如您将在上面看到的,OBJ 2 和 OBJ 1 对应于 IRBid 和 id。
What I need is to merge the two OBJ where IRBId OBJ 2 === id OBJ 1
我需要的是合并两个 OBJ,其中
IRBId OBJ 2 === id OBJ 1
The result I would expect after the merge is合并后我期望的结果是
OBJ merged OBJ合并
{
[{
"id":"93",
...
"IRBId":"9",
"irb": {
"name":"Again ",
...
}
},
{
"id":"92",
...
"IRBId":"8",
"irb": {
"name":"Admin ",
...
}
},
{
"id":"91",
...
"IRBId":"8",
"irb": {
"name":"Admin ",
...
}
],
},
I don't know how to make it looks like this.我不知道如何让它看起来像这样。
You need to use the map function on the nodes in the first object to construct a new object that contains the second and first object's attributes.您需要在第一个 object 中的节点上使用 map function 来构造一个包含第二个和第一个对象属性的新 object。
const obj1 = { "data": { "IRBs": { "nodes": [{ "id": "8", "obj1": "one", "name": "Admin ", }, { "id": "9", "obj1": "two", "name": "Again", } ] } } }; const obj2 = { "data": { "informedConsentForms": { "count": 3, "nodes": [{ "id": "93", "obj2": "1", "IRBId": "9", }, { "id": "92", "obj2": "2", "IRBId": "8", }, { "id": "91", "obj2": "3", "IRBId": "8", } ], } } }; const obj1Data = obj1.data.IRBs.nodes; const obj2Data = obj2.data.informedConsentForms.nodes; const res = obj2Data.map(item => { const obj1Item = obj1Data.find(obj1Item => item.IRBId === obj1Item.id); return obj1Item? {...item, "irb": {...obj1Item}}: {...item}; }); console.log(res);
Try using Array.reduce
Logic尝试使用
Array.reduce
逻辑
const obj1 = { "data": { "IRBs": { "nodes": [ { "id": "8", "name": "Admin ", }, { "id": "9", "name": "Again", } ], } } } const obj2 = { "data": { "informedConsentForms": { "count": 3, "nodes": [ { "id": "93", "IRBId": "9", }, { "id": "92", "IRBId": "8", }, { "id": "91", "IRBId": "8", } ], } }, }; const obj1List = obj1.data.IRBs.nodes; const output = obj2.data.informedConsentForms.nodes.reduce((acc, curr) => { const matchingNode = obj1List.find((item) => item.id === curr.IRBId); if (matchingNode) { acc.push({ id: curr.id, IRBId: curr.IRBId, irb: { name: matchingNode.name } }) } return acc; }, []); console.log(output);
i am using nested loop, try this one我正在使用嵌套循环,试试这个
const obj2 = { "data":{ "informedConsentForms":{ "count":3, "nodes":[ { "id":"93", "IRBId":"9", }, { "id":"92", "IRBId":"8", }, { "id":"91", "IRBId":"8", } ], } }, } const obj1 = { "data":{ "IRBs":{ "nodes":[ { "id":"8", "name":"Admin ", }, { "id":"9", "name":"Again", } ], } } } const result = []; const obj2Nodes = obj2.data.informedConsentForms.nodes; for(let i = 0; i < obj2Nodes.length; i++) { const obj1Nodes = obj1.data.IRBs.nodes for(let j = 0; j < obj1Nodes.length; j++) { if(obj2Nodes[i].IRBId === obj1Nodes[j].id) { const {id, ...reObj1Nodes} = obj1Nodes[j]; result.push({...obj2Nodes[i], 'irb': {...reObj1Nodes } }) } } } console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.