组合JSON数据集 [英] Combining JSON Data Sets
本文介绍了组合JSON数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下两个数据集:
Let's say I have the following two data sets:
{ "food": {
"apple": {
"color": "red"
},
"orange": {
"color": "orange"
},
"potato": {
"color": "brown"
},
"tomato": {
"color": "red"
}
}
}
和
{ "fruits": {
"apple": {
"isFruit": "yes"
},
"orange": {
"isFruit": "yes"
},
"tomato": {
"isFruit": "yes"
}
}
}
有没有办法将这两个组合起来,使它们最终看起来像: / p>
Is there a way to combine these two sets so that they end up looking like:
{ "food": {
"apple": {
"color": "red",
"isFruit": "yes"
},
"orange": {
"color": "orange",
"isFruit": "yes"
},
"potato": {
"color": "brown"
}
"tomato": {
"color": "red",
"isFruit": "yes"
}
}
}
我认为必须有一些方法来解析这两个集并将它们组合起来,但我还没弄清楚如何。
I assume there must be some way to parse these two sets and combine them but I haven't been able to figure out how.
推荐答案
尝试JavaScript 三元运算符 :
Try JavaScript ternary operator :
var firstJSON = {
"food": {
"apple": {
"color": "red"
},
"orange": {
"color": "orange"
},
"potato": {
"color": "brown"
},
"tomato": {
"color": "red"
}
}
};
var secondJSON = {
"fruits": {
"apple": {
"isFruit": "yes"
},
"orange": {
"isFruit": "yes"
},
"tomato": {
"isFruit": "yes"
}
}
};
for(var i in firstJSON.food) {
secondJSON.fruits.hasOwnProperty(i) ? firstJSON.food[i].isFruit = secondJSON.fruits[i].isFruit : '';
}
console.log(firstJSON);
这篇关于组合JSON数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文