欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js 判断两个对象是否相等

程序员文章站 2022-03-02 13:05:00
...
	//判断两个对象是否相等
	function isObjectValueEqual(a, b) {
        // Of course, we can do it use for in 
        // Create arrays of property names
        var aProps = Object.getOwnPropertyNames(a);
        var bProps = Object.getOwnPropertyNames(b);
     
        // If number of properties is different,
        // objects are not equivalent
        if (aProps.length != bProps.length) {
            return false;
        }
     
        for (var i = 0; i < aProps.length; i++) {
            var propName = aProps[i];
     
            // If values of same property are not equal,
            // objects are not equivalent
            if (a[propName] !== b[propName]) {
                return false;
            }
        }
     
        // If we made it this far, objects
        // are considered equivalent
        return true;
    }