我有两个不同的JSON对象,一个
[{"pmid":"29092960","MemberID":"33"},{"pmid":"28652336","MemberID":"33"}]
和另一个类似
{
"meta": {
"refs": 0,
"pmids": "29092960,28652336"
},
"links": {
"self": "https://icite.od.nih.gov/api/pubs?refs=0&pmids=23456789%2C27599104"
},
"data": [
{
"pmid": 29092960,
"doi": "10.1002/cncr.27976",
"authors": "ABC, XYZ",
"journal": "Cancer",
"year": 2013
},
{
"pmid": 28652336,
"doi": "10.1371/journal.pbio.1002541",
"authors": "Bbbb",
"journal": "PLoS Biol.",
"year": 2016
}
]
}
是否可以将这些组合?我创建的第一个对象是通过循环网格和添加项目。第二个来自我需要添加其他数据的api。这看起来与我发现的任何其他示例的结构都不一样。我想基于pmid值作为键进行链接
我希望输出为
{"pmid": 29092960,
"doi": "10.1002/cncr.27976",
"authors": "ABC, XYZ",
"journal": "Cancer",
"year": 2013,
"MemberID":"33"},{
"pmid": 29092960,
"doi": "10.1002/cncr.27976",
"authors": "ABC, XYZ",
"journal": "Cancer",
"year": 2013,
"MemberID": "33"}
答案 0 :(得分:2)
合并两个JSON对象javaScript
let arr1 =[{"pmid":"29092960","MemberID":"33"},{"pmid":"28652336","MemberID":"33"}];
let arr2 = {
"meta": {
"refs": 0,
"pmids": "29092960,28652336"
},
"links": {
"self": "https://icite.od.nih.gov/api/pubs?refs=0&pmids=23456789%2C27599104"
},
"data": [
{
"pmid": 29092960,
"doi": "10.1002/cncr.27976",
"authors": "ABC, XYZ",
"journal": "Cancer",
"year": 2013
},
{
"pmid": 28652336,
"doi": "10.1371/journal.pbio.1002541",
"authors": "Bbbb",
"journal": "PLoS Biol.",
"year": 2016
}
]
};
let arr3 = [];
arr1.forEach((itm, i) => {
arr3.push(Object.assign({}, itm, arr2.data.find(k => k.pmid==itm.pmid)));
});
console.log(arr3);
答案 1 :(得分:1)
您确定可以组合对象。
在循环中,这将帮助您完成工作,并且在处理对象时将其放在口袋中是一件好事。
我在下面提供了一个解决方案。
var obj1 = [{"pmid":"29092960","MemberID":"33"},{"pmid":"28652336","MemberID":"33"}];
var obj2 = {
"meta": {
"refs": 0,
"pmids": "29092960,28652336"
},
"links": {
"self": "https://icite.od.nih.gov/api/pubs?refs=0&pmids=23456789%2C27599104"
},
"data": [
{
"pmid": 29092960,
"doi": "10.1002/cncr.27976",
"authors": "ABC, XYZ",
"journal": "Cancer",
"year": 2013
},
{
"pmid": 28652336,
"doi": "10.1371/journal.pbio.1002541",
"authors": "Bbbb",
"journal": "PLoS Biol.",
"year": 2016
}
]
};
for(var char in obj1){
for(var innerArr in obj2.data){
// obj2 pmid is a number, so convert it to a string for the compare
if(obj1[char].pmid === obj2.data[char].pmid.toString()){
obj2.data[char].MemberID = obj1[char].MemberID;
}
}
}
console.log(JSON.stringify(obj2.data));
答案 2 :(得分:1)
结合使用 Array.map() 和 Object.Assign() :
let arr = [
{"pmid":"29092960","MemberID":"33"},
{"pmid":"28652336","MemberID":"33"}
];
let json = {
"meta": {
"refs": 0,
"pmids": "29092960,28652336"
},
"links": {
"self": "https://icite.od.nih.gov/api/pubs?refs=0&pmids=23456789%2C27599104"
},
"data": [
{
"pmid": 29092960,
"doi": "10.1002/cncr.27976",
"authors": "ABC, XYZ",
"journal": "Cancer",
"year": 2013
},
{
"pmid": 28652336,
"doi": "10.1371/journal.pbio.1002541",
"authors": "Bbbb",
"journal": "PLoS Biol.",
"year": 2016
}
]
};
let res = arr.map(({pmid, MemberID}) =>
{
return Object.assign({pmid, MemberID}, json.data.find(o => o.pmid === +pmid));
});
console.log(res);
请注意, unary plus operator 用于将原始数组的pmid
强制(或强制转换)为一个数字。从参考中您可以阅读:
一元加号是将某物转换为数字的最快且首选的方法
但是,与其他答案一样,您可以使用o.pmid == pmid
代替o.pmid === +pmid
。