码迷,mamicode.com
首页 > 其他好文 > 详细

判断两个对象相等

时间:2017-06-21 11:46:18      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:apr   prot   ++   ring   return   rda   相等   equal   str   

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

判断两个对象相等

标签:apr   prot   ++   ring   return   rda   相等   equal   str   

原文地址:http://www.cnblogs.com/mazhaokeng/p/7058027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!