zoukankan      html  css  js  c++  java
  • js 判断2个对象的值是否相等

    function isObjectValueEqual(a, b) {
        if((a == null && b != null) || (b == null && a != null)){
            return false;
        }
        console.info((a instanceof Array) + "<-x>" + (b instanceof Array));
        if(a instanceof Array && b instanceof Array){
            if(a.length != b.length){
                return false;
            }
            for (var i = 0; i < a.length; i++) {
                var aEle = a[i];
                var bEle = b[i];
                console.info(aEle + "<-xx>" + bEle);
                if(aEle.constructor == Object && bEle.constructor == Object){
                    if(!isObjectValueEqual(aEle, bEle)){
                        return false;
                    }
                } else if (aEle !== bEle) {
                    return false;
                }
            }
            for (var i = 0; i < b.length; i++) {
                var aEle = a[i];
                var bEle = b[i];
                console.info(aEle + "<-xxx>" + bEle);
                if(aEle.constructor == Object && bEle.constructor == Object){
                    if(!isObjectValueEqual(aEle, bEle)){
                        return false;
                    }
                } else if (aEle !== bEle) {
                    return false;
                }
            }
        } else if(a.constructor == Object && b.constructor == Object){
            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.info(propName + ":" + a[propName] + "<->" + b[propName], a[propName].constructor, b[propName].constructor);
                if(a[propName].constructor == Object && b[propName].constructor == Object){
                    if(!isObjectValueEqual(a[propName], b[propName])){
                        return false;
                    }
                } else if (a[propName] instanceof Array && b[propName] instanceof Array) {
                    if(!isObjectValueEqual(a[propName], b[propName])){
                        return false;
                    }
                } else if (a[propName] !== b[propName]) {
                    return false;
                }
            }
            for (var i = 0; i < bProps.length; i++) {
                var propName = bProps[i];
                console.info(propName + ":" + a[propName] + "<-->" + b[propName]);
                if(a[propName].constructor == Object && b[propName].constructor == Object){
                    if(!isObjectValueEqual(a[propName], b[propName])){
                        return false;
                    }
                } else if (a[propName] instanceof Array && b[propName] instanceof Array) {
                    if(!isObjectValueEqual(a[propName], b[propName])){
                        return false;
                    }
                } else if (a[propName] !== b[propName]) {
                    return false;
                }
            }
        }
        return true;
    }
  • 相关阅读:
    管道命令'|' 和xargs find命令找到后把所有找到的删除
    UVa
    【手势交互】9. PS Move
    jquery时间格式化插件
    Android学习路线(十三)Activity生命周期——暂停和恢复(Pausing and Resuming )一个Activity
    hdu 2604 Queuing (矩阵高速幂)
    【Linux驱动】TQ2440 DM9000E网卡驱动移植(Linux-2.6.30.4)
    bzoj2648 SJY摆棋子
    Hive编程指南_学习笔记01
    通信协议中的转义符
  • 原文地址:https://www.cnblogs.com/huanglisong/p/14066913.html
Copyright © 2011-2022 走看看