如何比较2个对象是否相同

较为简单的方法,如下

//比较2个对象是否相同
function isObjEqual(o1,o2){
    var props1 = Object.getOwnPropertyNames(o1);
    var props2 = Object.getOwnPropertyNames(o2);
    if (props1.length != props2.length) {
        return false;
    }
    for (var i = 0,max = props1.length; i < max; i++) {
        var propName = props1[i];
        if (o1[propName] !== o2[propName]) {
            return false;
        }
    }
    return true;
}
var obj1 = {id:1,name:"张三"}
var obj2 = {id:2,name:"李四"}
var obj3 = {id:1,name:"张三",age:25}
var obj4 = {id:1,name:"张三"}
console.log(isObjEqual(obj1,obj2));//false
console.log(isObjEqual(obj1,obj3));//false
console.log(isObjEqual(obj1,obj4));//true

扩展写法:

Object.prototype.vequal = function(obj){
    var props1 = Object.getOwnPropertyNames(this);
    var props2 = Object.getOwnPropertyNames(obj);
    if (props1.length != props2.length) {
        return false;
    }
    for (var i = 0,max = props1.length; i < max; i++) {
        var propName = props1[i];
        if (this[propName] !== obj[propName]) {
            return false;
        }
    }
    return true;
}
var obj1 = {id:1,name:"张三"}
var obj2 = {id:2,name:"李四"}
var obj3 = {id:1,name:"张三",age:25}
var obj4 = {id:1,name:"张三"}
console.log(obj1.equal(obj2));//false
console.log(obj1.equal(obj3));//false
console.log(obj1.equal(obj4));//true

参考:https://blog.csdn.net/yqwang75457/article/details/62226145

01-16 18:43