欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

angular判断两个对象值是否相等,包含对象深层判断,深拷贝的两个对象

程序员文章站 2022-03-02 13:04:54
...

angular

    public isEqual(obj1:any,obj2:any):boolean{
        if(!(obj1 instanceof Object) || !(obj2 instanceof Object)){/*  判断不是对象  */
            return obj1 === obj2;
        }
        if(Object.keys(obj1).length !== Object.keys(obj2).length){
            return false;
            //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,判断键值个数是否相等
        }
        for(var attr in obj1){//逐个值进行判断
            if(obj1[attr] instanceof Object && obj2[attr] instanceof Object){
                return this.isEqual(obj1[attr],obj2[attr]);//如果值为对象再递归进行判断
            }else if(obj1[attr] !== obj2[attr]){
                return false;
            }
        }
        return true;
    }

这是我用来比较深拷贝的两个对象

        this.info = $.extend(true, {}, d);//复制对象
        this.standByInfo = $.extend(true, {}, d);//复制对象

下面是我的angular对象类,套了四层,info修改后可以判断出对象内容不一样

export class ModbusInfo {
    public id;
    public ruleNumber;
    public explain;
    public funcParam:FuncParam[] = [];
}
export class FuncParam{
    constructor(id, functionCode, explain,dontOperate,InstructionFormatCheck) {
        this.id = id;
        this.functionCode = functionCode;
        this.explain = explain;
        this.InstructionFormatCheck = InstructionFormatCheck;
        this.dontOperate = dontOperate;
    }
    public id;
    public functionCode;
    public explain;
    public dontOperate:boolean = false;
    public InstructionFormatCheck:boolean = false;
    temp : Temp = new Temp();
}

export class Temp01 {
    startaddr: number = 0;
    endaddr: number = 0;
    explanate: string = "";
}

export class Temp02 {
    valueaddr: number = 0;
    min: number = 0;
    max: number = 0;
    explanate: string = "";
}

export class Temp {
    temp01: Temp01[] = [];
    temp02: Temp02[] = [];
}

参考:https://blog.csdn.net/sunny123x/article/details/88051298
感谢!

相关标签: 前端 js angular