美文网首页
JavaScript通过遍历所有属性判断两个Object对象是否

JavaScript通过遍历所有属性判断两个Object对象是否

作者: b83bde1247ec | 来源:发表于2020-07-13 10:29 被阅读0次

function isEquivalent(a, b) {

          // 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;

}

// Outputs: true

console.log(isEquivalent(bobaFett, jangoFett));

相关文章

网友评论

      本文标题:JavaScript通过遍历所有属性判断两个Object对象是否

      本文链接:https://www.haomeiwen.com/subject/kzqanxtx.html