1

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.

1
  • Compare them using .length method on arrays. Commented Apr 21, 2017 at 22:52

3 Answers 3

2

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, lodash is an amazing tool. Love it.
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.