Reputation: 369
Consider such situation: I have two complex jsons - one has a full list of properties and the other is theoretically the same, but can be missing some values. I need to overwrite the values in the first one using the partial one.
How can I achieve this? I tried something like this:
function ObjectValues(data) {
var isArray = data instanceof Array;
for (var key in data) {
if (data.hasOwnProperty(key)) {
if (typeof (data[key]) == "object") {
if (!isArray) {
profileData[key] = data[key];
}
ObjectValues(data[key]);
} else if (!isArray) {
}
}
}
}
ObjectValues(data);
the idea is to loop through the partial json (data) and overwrite corresponding values in profileData. This function can loop through the structure all right (whenever there's an object within an object it starts itself on this level), but I don't know how to make it overwrite the values on each level, as profileData[key] = data[key] is not sufficient
Edit: a simple example would be:
Data = {
"activity": {
"online": [1,1,3,4,0,1,0,3,2],
}
},
"location": "Antarctica",
}
profileData = {
"activity": {
"online": [],
}
},
"location": "N/A",
}
right now, I'd get
profileData = {
"activity": {
"online": [],
}
},
"online": [1,1,3,4,0,1,0,3,2],
"location": "Antarctica",
}
it works fine on the first level, but then it obviously just appends deeper keys to the root (as you would expect). The problem is retrieving the key path in the subiterations.
Upvotes: 0
Views: 52