0

Let's say I have the following two data sets:

{ "food": {
    "apple": {
        "color": "red"
        },
    "orange": {
        "color": "orange"
        },
    "potato": {
        "color": "brown"
        },
    "tomato": {
        "color": "red"
        }
    }
}

and

{ "fruits": {
    "apple": {
        "isFruit": "yes"
        },
    "orange": {
        "isFruit": "yes"
       },
    "tomato": {
        "isFruit": "yes"
       }
    }
}

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.

David R
  • 14,711
  • 7
  • 54
  • 72
ch1maera
  • 1,369
  • 5
  • 20
  • 42

4 Answers4

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);
Bartek Fryzowicz
  • 6,464
  • 18
  • 27
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);

Check this fiddle

This will work even if you add some more properties in fruitsData for fruit.

Harshal
  • 961
  • 1
  • 11
  • 26
2

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);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
1

Try below solution and it will work all JSON object in generic way. and also i handled for dynamic length of objects too.

I hope this will help you.

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());

Please find the working demo in the below fiddle.

Working Demo

Victor AJ
  • 107
  • 1
  • 4