The example in this article tells you how JavaScript compares two objects for equality. Share to everyone for your reference. Specifically as follows:
In Python, you can use the CMP () built-in function to compare two objects for equality (arrays, sequences, dictionaries). However, there is no relevant implementation in the JavaScript language. This JS code by the JS object in various aspects of the comparison to determine whether two objects are equal
CMP = function (x, y) {//If both x and y are null or undefined and exactly the same If (x = = y) {return true; }//If they are not strictly equal, they both-need to IS Objects If (! (x instanceof Object) | | ! (y instanceof Object))
{return false;
}//they must have the exact same prototype chain,the we can do are closest the//test.
if (x.constructor!== y.constructor) {return false; For (var p in x) {//inherited properties were tested using X.constructor = = Y.constructor if (x.hasownpropert Y (P)) {//allows comparing x[p] and y[p] When set to undefined if (! Y.hasownproperty (p)) {return fals
E
}//If they have the same strict value or identity then they-are equal If (x[p] = = y[P]) {continue; }//Numbers, Strings, functions, booleans must be strictly equal if (typeof (X[p])!== "Object") {return FA
Lse }//Objects and Arrays must be tested recursively if (! Object.Equals (x[p], y[p]) {return false; (p in Y) {//allows x[P] to is set to undefined if (Y.hasownproperty (p) &&! X.HASOWNPR
Operty (p)) {return false;
} return true; };
Use:
obja={
A: ' 123 ',
B: ' 456 '
};
objb={
A: ' 123 ',
B: '
};
var isequal= cmp (Obja, OBJB);
Console.log (isequal); False is not the same
I hope this article will help you with your JavaScript programming.