我们说我有以下两个数据集:
{ "fruits": {
"apple": {
"isFruit": "yes"
},
"orange": {
"isFruit": "yes"
},
"tomato": {
"isFruit": "yes"
}
}
}
和
{ "food": {
"apple": {
"color": "red",
"isFruit": "yes"
},
"orange": {
"color": "orange",
"isFruit": "yes"
},
"potato": {
"color": "brown"
}
"tomato": {
"color": "red",
"isFruit": "yes"
}
}
}
有没有办法将这两个组合起来,使它们最终看起来像:
{{1}}
我认为必须有一些方法来解析这两组并将它们组合起来,但我还没有弄清楚如何。
答案 0 :(得分:4)
var foodsData = { "food": {
"apple": {
"color": "red"
},
"orange": {
"color": "orange"
},
"potato": {
"color": "brown"
},
"tomato": {
"color": "red"
}
}
};
var fruitsData = { "fruits": {
"apple": {
"isFruit": "yes"
},
"orange": {
"isFruit": "yes"
},
"tomato": {
"isFruit": "yes"
}
}
};
for (f in foodsData.food) {
if (fruitsData.fruits.hasOwnProperty(f)) {
foodsData.food[f].isFruit = fruitsData.fruits[f].isFruit
}
}
console.log(foodsData);
答案 1 :(得分:2)
var foodsData = {
"food": {
"apple": {
"color": "red"
},
"orange": {
"color": "orange"
},
"potato": {
"color": "brown"
},
"tomato": {
"color": "red"
}
}
};
var fruitsData = {
"fruits": {
"apple": {
"isFruit": "yes",
"isRoot": 'no',
"seasonal": 'Yes',
},
"orange": {
"isFruit": "yes",
"isRoot": 'no'
},
"tomato": {
"isFruit": "yes",
"isRoot": 'No',
"seasonal": 'No',
}
}
};
for (fruit in foodsData.food) {
if (fruitsData.fruits.hasOwnProperty(fruit)) {
var allProperties = Object.keys(fruitsData.fruits[fruit]);
for (var i = 0; i < allProperties.length; i++) {
var property = allProperties[i];
foodsData.food[fruit][property] = fruitsData.fruits[fruit][property];
}
}
}
console.log(foodsData);
选中此fiddle
即使你在fruitData中为fruit添加了一些属性,这也会有效。
答案 2 :(得分:2)
尝试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);
答案 3 :(得分:1)
尝试以下解决方案,它将以通用方式工作所有JSON对象。 而且我也处理了物体的动态长度。
我希望这会对你有所帮助。
var obj1 = { "food": {
"apple": {
"color": "red"
},
"orange": {
"color": "orange"
},
"potato": {
"color": "brown"
},
"tomato": {
"color": "red"
}
}
}
var obj2 = { "fruits": {
"apple": {
"isFruit": "yes"
},
"orange": {
"isFruit": "yes"
},
"tomato": {
"isFruit": "yes"
}
}
}
function result() {
var resObj = [];
if(objSize(obj1[Object.keys(obj1)]) >= objSize(obj2[Object.keys(obj2)])) {
var indexObj1 = 0;
var indexObj2 = 0;
for(var i in obj1[Object.keys(obj1)]) {
var flag= false;
indexObj1++;
indexObj2 = 0;
for(var j in obj2[Object.keys(obj2)]) {
indexObj2++;
if(indexObj2 == indexObj1){
if(i == j) {
var item = [];
var item1 = {};
item.push(obj1[Object.keys(obj1)][i]);
item.push(obj2[Object.keys(obj2)][j]);
item1[i] = item;
resObj.push(item1);
flag = true;
break;
} else {
var item = [];
var item1 = {};
item.push(obj1[Object.keys(obj1)][i]);
item1[i] = item;
resObj.push(item1);
item = [];
item1 = {};
item.push(obj2[Object.keys(obj2)][j]);
item1[j] = item;
resObj.push(item1);
flag = true;
break;
}
}
}
if(!flag) {
var item1 = {};
item.push(obj1[Object.keys(obj1)][i]);
item1[i] = item;
resObj.push(item1);
flag = true;
}
}
} else {
for(var i in obj2[Object.keys(obj2)]) {
var flag= false;
for(var j in obj1[Object.keys(obj1)]) {
if(i == j) {
var item = [];
var item1 = {};
item.push(obj1[Object.keys(obj1)][i]);
item.push(obj2[Object.keys(obj2)][j]);
item1[i] = item;
resObj.push(item1);
flag = true;
break;
}
}
if(!flag) {
var item1 = {};
item.push(obj1[Object.keys(obj1)][i]);
item1[i] = item;
resObj.push(item1);
flag = true;
}
}
}
return resObj;
}
function objSize(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
}
console.log(result());
请在下面的小提琴中找到工作演示。