I have 2 Json's object (this is an exemple, my object can have array in array in object, etc....):
var obj = jQuery.parseJSON('{
"name" : "John",
"lastname" : "Doe",
"birthday" : "05/20/1980",
"fav_game" : [
"tetris",
"tic tac toe"
],
"best_friends" : {
"name" : "Jane",
"lastname" : "Murdoc"
}
}');
var obj2 = jQuery.parseJSON('{
"name" : "John",
"lastname" : "Doe_update",
"birthday" : "05/20/1980",
"fav_game" : [
"tetris",
"tic tac toe_update"
],
"best_friends" : {
"name" : "Jane",
"lastname" : "Murdoc",
"new_prop" : "update"
}
}');
I would like get a difference between this 2 object for create a new one :
var obj3 = diff(obj, obj2);
Obj3 should looks like it :
{
"lastname" : "Doe_update",
"fav_game": [
null,
"tic tac toe_update"
],
"best_friends" : {
"new_prop" : "update"
}
}
I found a script and i'm trying to modificate it for my need, but i'm not good enougth :
function filter(obj1, obj2) {
var result = {};
for(key in obj1) {
if(obj2[key] != obj1[key]) {
result[key] = obj2[key];
}
if(typeof obj2[key] == 'array' && typeof obj1[key] == 'array') {
result[key] = arguments.callee(obj1[key], obj2[key]);
}
if(typeof obj2[key] == 'object' && typeof obj1[key] == 'object') {
result[key] = arguments.callee(obj1[key], obj2[key]);
}
}
return result;
}
And the result using the script is :
{
"lastname" : "Doe_update",
"fav_game": [
null,
"tic tac toe_update"
],
"best_friends" : {
}
}
I need to deal with added value...
I see many things by searching, but never the same thing of what i need. Sorry for my low level english, update are welcome. Thanks you guys !