javascript實現deepEqual和shallowEqual

看風景就發表於2018-09-17
function deepEqual(x, y) {
    if (x === y) {
        return true;
    }
    if (!(typeof x == "object" && x != null) || !(typeof y == "object" && y != null)){
        return false;
    }
    //比較物件內部
    if (Object.keys(x).length != Object.keys(y).length){
        return false;
    }
    for (var prop in x) {
        if (y.hasOwnProperty(prop))
        {  
            if (!deepEqual(x[prop], y[prop])){
                return false;
            }
        }
        else{
            return false;
        }
    }
    return true;
}
function shallowEqual(objA,objB){
    if(objA === objB){
        return true;
    }
    if(!(typeof objA === 'object' && objA != null) || !(typeof objB === 'object' && objB != null)){
        return false;
    }
    const keysA = Object.keys(objA);
    const keysB = Object.keys(objB);
    if(keysA.length !== keysB.length){
        return false;
    }
    for(let i = 0;i< keysA.length;i++){
        if(objB.hasOwnProperty(keysA[i])){
            if(objA[keysA[i]] !== objB[keysA[i]]){
                return false;
            }
        }
        else{
            return false;
        }
    }
    return true;
}

 

 

參考:https://stackoverflow.com/questions/25456013/javascript-deepequal-comparison#

相關文章