<script> var a = { name: ‘jk‘, age: 12, say: { today: ‘hi‘, yesterday: ‘no‘ }, arr: [1,2] }; var b = { name: ‘jk‘, age: 12, say: { today: ‘hi‘, yesterday: ‘no‘ }, arr: [1,2] }; console.log(isObjectValueEqual(a, b)); function isObjectValueEqual(a, b) { if (typeof a == ‘number‘ && typeof b == ‘number‘) { return a == b } var aProps = Object.getOwnPropertyNames(a); var bProps = Object.getOwnPropertyNames(b); if (aProps.length != bProps.length) { return false; } for (var i = 0; i < aProps.length; i++) { var propName = aProps[i]; console.log(propName, Object.prototype.toString(a[propName])); //if (Object.prototype.toString(a[propName]) == ‘[Object Object]‘ || Object.prototype.toString(b[propName]) == ‘[Object Object]‘) { if ( typeof a[propName] === ‘object‘ || typeof b[propName] === ‘object‘ ){ console.log(a[propName], b[propName]); isObjectValueEqual(a[propName], b[propName]) }else if (a[propName] !== b[propName]) { return false; } } return true; }</script>
时间: 2024-10-15 13:10:55