/**
* 用於遞歸判斷兩個對象是否一致,以區分是否修改
* @param {*} oldObj
* @param {*} newObj
* 返回false:不相等
*/
export function objsDiffer(oldObj, newObj){
let aProps = Object.getOwnPropertyNames(oldObj);
let bProps = Object.getOwnPropertyNames(newObj);
if (aProps.length != bProps.length) {
return false;
}
//return 時for中斷
for (var i = 0; i < aProps.length; i++) {
let propName = aProps[i];
let propA = oldObj[propName];
let propB = newObj[propName];
if (propA == propB) {
if ( propA instanceof Object ) {
if (!objsDiffer(propA, propB)) {
return false
}
} else if ( propA instanceof Array && propB instanceof Array ) {
if( propA.length != propB.length ){
return false
}
let tem = arrDiffer(propA, propB)
if(!tem) return false;
}
} else {
return false
}
}
return true;
}
/**
* 返回false 表示不相等
*/
export function arrDiffer(oldObj, newObj){
for (var i = 0; i < oldObj.length; i++) {
let propA = oldObj[propName];
let propB = newObj[propName];
if (propA == propB) {
if ( propA instanceof Object) {
if (!objsDiffer(propA, propB)) {
return false
}
} else if ( propA instanceof Array && propB instanceof Array ) {
if( propA.length != propB.length ){
return false
}
let temp = arrDiffer(propA, propB)
if(!temp) return false
}
} else {
return false
}
}
return true
}
複製代碼