• 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;
    }
  • 相关阅读:
    vue 路由
    CSS篇-dispaly、position、定位机制、布局、盒子模型、BFC
    CSS篇-样式表、选择器、权重、伪类
    vue-cli3.0 开发环境构建
    Vue 全家桶学习资源(转)
    vue全家桶常用命名
    服务拆分
    分布式事务及其解决方法
    java mvc 及其缓存
    java 面试经典题
  • 原文地址:https://www.cnblogs.com/huanglisong/p/14066913.html
Copyright © 2020-2023  润新知