深度比较

let obj1 = {
    a:111,
    b:"hahah",
    c:{
        name:"qaz",
        age:45
    },
    d:[1,2,3,4]
}
let obj2 = {
    a: 111,
    b: "hahah",
    c: {
        name: "qaz",
        age: 45
    },
    d: [1, 2, 3, 5]
}
let obj3 = 1111;
let obj4 = 1111;
console.log(obj1===obj2)//false
console.log(objSame(obj1, obj2))//true

function objType(obj){
    return typeof obj !='object' && obj !=null ? true: false;
}

function objSame(obj1, obj2){
    if (objType(obj1) || objType(obj2)){
        return obj1 === obj2;
    }
    if (obj1 === obj2){
        return true;
    }
    let key1 = Object.keys(obj1);
    let key2 = Object.keys(obj2);
    if (key1.length != key2.length){
        return false;
    }
    for (let i in obj1){
        let res = objSame(obj1[i], obj2[i])
        if (!res){
            return false;
        }
    }
    return true;
}
原文地址:https://www.cnblogs.com/chenlw/p/12689694.html