本帖最后由 夕阳啊丶 于 2019-12-26 13:29 编辑
funDifference(object, other) {
let diff = {};
let vChildren;
for (let key in object) {
if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
if (Object.prototype.toString.call(object[key]) === '[object Array]') {
vChildren = object[key]
} else {
vChildren = this.funDifference(object[key], other[key]);
}
if (Object.keys(vChildren).length > 0) {
diff[key] = vChildren;
}
} else if (object[key] !== other[key]) {
diff[key] = object[key];
}
}
return diff;
},
exp:
let object1 = {
firstname: 'John',
lastname: 'Cena',
privateInfo: {
privateProperty1: false,
privateProperty2: ['20%', '600%', 'test_test', ['one', 'two', 'three', ['one1', 'two1', 'three2']], 'hahaha',{Obj1:{a:1,b:2}}],
test: [1, 2, 3, [11, 22, 33]]
}
};
let object2 = {
firstname: 'John',
lastname: 'Csena',
privateInfo: {
privateProperty1: true,
privateProperty2: ['20%', '60%', 'test_test', ['one', 'two', 'three', ['one1', 'two1', 'three1']], 'hahaha',{Obj1:{a:1,b:4}}],
test: [1, 2, 3, [11, 22, 44]]
}
};
console.log(this.funDifference(object2, object1));
[JavaScript] 纯文本查看 复制代码 funDifference(object, other) {
let diff = {};
let vChildren;
for (let key in object) {
if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
if (Object.prototype.toString.call(object[key]) === '[object Array]') {
vChildren = object[key]
} else {
vChildren = this.funDifference(object[key], other[key]);
}
if (Object.keys(vChildren).length > 0) {
diff[key] = vChildren;
}
} else if (object[key] !== other[key]) {
diff[key] = object[key];
}
}
return diff;
}, |