user7531085
user7531085

Reputation:

Checking if 2 Arrays of Objects are exactly the same (length, properties, values)

I would like to see if 2 arrays of objects are exactly the same as one another in terms of array length, object properties, and object values. If not, i would like to return false. These objects are not direct clones. However, they always have the same properties, but their values can change. I will be performing this check every 45 seconds to check for changes.

My example:

var obj1 = [{
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        },
        {
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        },
        ];

var obj2 = [{
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        }];

In my case, I want obj1 === obj2 to return false because even though they have the same properties and values, the array length is different.

Upvotes: 1

Views: 70

Answers (3)

Manuel Otto
Manuel Otto

Reputation: 6540

Incase you don't want to use a 3rd party library you can use this function:

function compareObjects(obj1,obj2,reversed){
    for(var key in obj1){
        if(typeof obj2[key]=="undefined") return false
        if(typeof obj1[key] == "object"){
            if(!compareObjects(obj1[key],obj2[key])) return false
        }
        else if(obj1[key]!=obj2[key]) return false
    }
    return reversed ? true : compareObjects(obj2,obj1,true)
}

Use it by calling compareObjects(obj1,obj2).

Upvotes: 1

Aravind
Aravind

Reputation: 41543

Use 3rd party library when you can't achieve in traditional js. You can simply use === or JSON.stringify()

// Code goes here

var obj1 = [{
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        },
        {
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        },
        ];

var obj2 = [{
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        }];
        

obj3 = obj2
console.log(obj2===obj3); //true
console.log( JSON.stringify(obj3)=== JSON.stringify(obj2)); // true

Upvotes: 1

stolli
stolli

Reputation: 5926

this is a job for lodash

var obj1 = [{
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        },
        {
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        },
        ];

var obj2 = [{
            id: "1234567",
            first_name: "",
            last_name: "",
            email: "[email protected]",
            company: null,
            notes: null,
            registrationType: "",
            alerts: [ ],
            reg_scan: null
        }];
        
console.log(_.isEqual(obj1, obj2))
// false

obj3 = _.cloneDeep(obj2)
console.log(_.isEqual(obj2, obj3))
// true
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Upvotes: 2

Related Questions