Skip to content

Instantly share code, notes, and snippets.

@imadbz
Forked from nicbell/1_primitive_comparison.js
Last active February 18, 2019 21:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imadbz/333d4aa9dd6cec1f8d6f58faccab9d19 to your computer and use it in GitHub Desktop.
Save imadbz/333d4aa9dd6cec1f8d6f58faccab9d19 to your computer and use it in GitHub Desktop.
Dropin replacement for '===' that supports deep objects compare. JavaScript object deep comparison.Comparing x === y, where x and y are values, return true or false. Comparing x === y, where x and y are objects, returns true if x and y refer to the same object. Otherwise, returns false even if the objects appear identical.Here is a solution to c…
//Primitive Type Comparison
var a = 1;
var b = 1;
var c = a;
console.log(a == b); //true
console.log(a === b); //true
console.log(a == c); //true
console.log(a === c); //true
//Object comparison
var a = { blah: 1 };
var b = { blah: 1 };
var c = a;
console.log(a == b); //false
console.log(a === b); //false
console.log(a == c); //true
console.log(a === c); //true
//How To Compare Object Values
var a = { blah: 1 };
var b = { blah: 1 };
var c = a;
var d = { blah: 2 };
var e = 5;
var f = 5;
var g = 6;
const Compare = function (obj1, obj2) {
// if they are not of the same type, don't bother
if(typeof (obj1) !== typeof (obj2)) return false;
// support non object types as well
if(typeof (obj1) != 'object')
return obj1 == obj2
//Loop through properties in object 1
for (var p in obj1) {
//Check property exists on both objects
if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;
switch (typeof (obj1[p])) {
//Deep compare objects
case 'object':
if (!Object.compare(obj1[p], obj2[p])) return false;
break;
//Compare function code
case 'function':
if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;
break;
//Compare values
default:
if (obj1[p] != obj2[p]) return false;
}
}
//Check object 2 for any extra properties
for (var p in obj2) {
if (typeof (obj1[p]) == 'undefined') return false;
}
return true;
};
console.log(Compare(a, b)); //true
console.log(Compare(a, c)); //true
console.log(Compare(a, d)); //false
console.log(Compare(e, f)); //true
console.log(Compare(e, g)); //false
@k7moorthi
Copy link

k7moorthi commented Jul 26, 2018

Fails with the below example:

  var oldObj = {
    "name": "John",
    "age": 30,
    "childs": ["Smith", "Steve", "Trent"],
    "vechicals": {
      "twoWheelers": ["Honda", "Bajaj", "Suzuki"],
      "fourWheelers": {
        "cars": {
          "count": 3,
          "details": [{
            "make": "bmw",
            "model": "123"
          }, {
            "make": "Ford",
            "model": "234"
          }]
        },
        "vans": {
        "count": 5
        }
      }
    }
  };

  var newObj = {
    "age": 30,
    "name": "John",
    "childs": ["Smith", "Steve", "Trent"],
    "vechicals": {
      "twoWheelers": ["Honda", "Bajaj", "Suzuki"],
      "fourWheelers": {
        "cars": {
          "count": 3,
          "details": [{
            "make": "Ford",
            "model": "234"
          }, {
            "make": "bmw",
            "model": "123"
          }]
        },
        "vans": {
        "count": 5
        }
      }
    }
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment