How to check if two objects coincide with JavaScript?

I need a function:

function isSame(a, b){ } 

In which, if a and b are the same, it returns true. , I tried return a === b , but found that [] === [] would return false.
Some of the results that I expect from this function may give:

 isSame(3.14, 3.14); // true isSame("hello", "hello"); // true isSame([], []); // true isSame([1, 2], [1, 2]); // true isSame({ a : 1, b : 2}, {a : 1, b : 2}); //true isSame([1, {a:1}], [1, {a:1}]); //true 
+8
javascript
source share
5 answers

There are several examples adapted from the diagram on Crockford's website. In particular, check:

 function isEqual(s1, s2) { return isAtom(s1) && isAtom(s2) ? isEqan(s1, s2) : isAtom(s1) || isAtom(s2) ? false : isEqlist(s1, s2); } 

All of this can be found here:

http://javascript.crockford.com/little.js

Here is a working example:

http://jsfiddle.net/FhGpd/

Update:

Just wrote some test cases based on OP. It turns out that I needed to change the function sub1 to check <= 0 not === 0, otherwise isEqual (3.14, 3.14) blew up the stack. Also, isEqual does not work for comparing objects, so you are on your own. However, if you follow the examples on the Crockford website, you will see how easy and fun it is to write recursive methods that could be used to verify the equality of objects.

+4
source share

You can insert Underscore.js and use _.isEqual(obj1, obj2) . The function works for arbitrary objects and uses everything that is the most effective way of checking these objects for equality.

+12
source share

the best way to do this is to use a JSON serializer. serialize both the string and compare the string.

+4
source share

Here is what might work:

 function isSame(obj1, obj2, prefer){ // Optional parameter prefer allows to only check for object keys and not both keys and values of an object var obj_prefer = prefer || "both"; function checkArray(arr1, arr2){ for(var i = 0, j = obj1.length; i<j; i++){ if(obj1[i] !== obj2[i]){return false;} } return true; } function checkValues(obj_1, obj_2){ for(var prop in obj_1){ if(typeof obj_1[prop] === "function"){ // converting functions to string so that they can be matched obj_1[prop] = String(obj_1[prop]); obj_2[prop] = String(obj_2[prop]); } if(obj_1[prop] !== obj_2[prop]){ return false;} } return true; } // The built in === will check everything except when typeof object is "object" if ( typeof obj1 === "object"){ // typeof Array is object so this is an alternative if((typeof obj1.push === "function") && (!obj1.hasOwnProperty('push'))){ return checkArray(obj1, obj2); } else{ if( obj_prefer !== "keys"){ // do not check for values if obj_prefer is "keys" return checkValues(obj1, obj2); } var keys_1 = Object.keys(obj1); var keys_2 = Object.keys(obj2); if(!checkArray(keys_1, keys_2)){return false;} return true; } } // I thought undefined === undefined will give false but it isn't so you can remove it if( typeof obj1 === "undefined" && typeof obj2 === "undefined" ){return true} if(typeof obj1 === "function"){ return String(obj1) === String(obj2); } return obj1 === obj2; } console.log(isSame(2, 2)); //true console.log(isSame([1], [1])); // true 

Since it converts functions to strings to compare them, check for spaces that might break things:

 var func1 = function(){}, func2 = function(){ }; // function with extra space isSame(func1, func2); // false 

You can check out http://jsfiddle.net/webholik/dwaLN/4/ to try it yourself.

0
source share

If anyone reads this answer, Angular.js , you can use angular.equals(obj1,obj2);

According to docs :

Determines the equivalence of two objects or two values. Supports value types, regular expressions, arrays, and objects. Two objects or values ​​are considered equivalent if at least one of the following is true:

  • Both objects or values ​​are compared === .
  • Both objects or values ​​are of the same type, and all their properties are equal, comparing them with angular.equals .
  • Both values ​​of NaN . (In JavaScript, NaN == NaN => false . But we assume that two NaN are equal).
  • Both values ​​represent the same regular expression (in JavaScript, /abc/ == /abc/ => false . But we consider two regular expressions as equal when their textual representation matches).
  • When comparing properties, properties of the function type and properties with names beginning with $ are ignored.

Scope and DOMWindow objects are compared only by identifier (===) .

0
source share

All Articles