JavaScript compares several examples of whether two objects are equal

Source: Internet
Author: User
Tags hasownproperty

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
if (! (x instanceof Object) | | ! (y instanceof Object)) {
return false;
}

They must have the exact same prototype chain,the closest we can do
Test the constructor.
if (x.constructor!== y.constructor) {
return false;
}

For (var p in x) {
Inherited properties were tested using X.constructor = = = Y.constructor
if (X.hasownproperty (p)) {
Allows comparing x[p] and y[p] when set to undefined
if (! Y.hasownproperty (p)) {
return false;
}

If they have the same strict value or identity then they are
if (x[p] = = y[P]) {
Continue
}

Numbers, Strings, functions, booleans must be strictly equal
if (typeof (X[p])!== "Object") {
return false;
}

Objects and Arrays must be tested recursively
if (! Object.Equals (x[p], y[p])) {
return false;
}
}
}

For (p in Y) {
Allows x[P] to is set to undefined
if (Y.hasownproperty (p) &&! X.hasownproperty (p)) {
return false;
}
}
return true;
};

Use:

obja={
A: ' 123 ',
B: ' 456 '
};
objb={
A: ' 123 ',
B: ' 000 '
};
var isequal= cmp (Obja, OBJB);
Console.log (isequal); False is not the same

We're going to do a more in-depth comparison.

It's a bit of a hassle to compare the members of two objects in JavaScript, but if it's just a first-tier comparison, it's easy, but the attribute in the child object may be an object, so it can only be recursive.

Code:


to Heavy
Array.prototype.unique = function () {
This.sort ();
var re=[this[0]];
for (var i = 1; i < this.length; i++) {
if (This[i]!== re[re.length-1]) {
Re.push (This[i]);
}
}
return re;
}

var O2O = function (O1,o2) {

if (typeof O1!= typeof O2) {
return false;
}

if (typeof O1.length!= typeof O2.length) {
return false;
}

var bool = true;

var keyArr1 = [];
var keyArr2 = [];

for (var i in O1) {
Keyarr1.push (i);
}

for (var i in O2) {
Keyarr2.push (i);
}

if (keyarr1.length!= keyarr2.length) {
return false;
}

For (Var i=0, k=keyarr2.length;i<k;i++) {
Keyarr1.push (Keyarr2[i]);
}

var Keyarr = Keyarr1.unique ();

for (Var i=0,k=keyarr.length;i<k;i++) {
if ((Keyarr[i] in O1) && (Keyarr[i] in O2)) {
if (typeof o1[keyarr[i]] = = = ' object ' && typeof o2[keyarr[i]] = = ' object ') {
BOOL = O2O (O1[keyarr[i]], o2[keyarr[i]);
}else if (O1[keyarr[i]]!== O2[keyarr[i]) {
return false;
}
}else{
return false;
}
}

return bool;
};

uses:

var O1 = {
    age:18,
    info: {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB Sp ' Author ': ' Lee ',
        ' job ': [
             ' A ', ' B '
       ]
   },
    ' name ': ' Laowu '
};
 
 
var o2 = {
    ' name ': ' Laowu ',
    ' age ': A,
&nbs p;   Info: {
        ' author ': ' Lee ',
    & nbsp;   ' job ': [
            ' a ',        
            ' B '
       ]
   }
};
 
Console.log (O2O (O1,O2));//True

Change the age of O1 to 18 of the string


var O1 = {
Age: "18",
Info: {
' Author ': ' Lee ',
' Job ': [
' A ', ' B '
]
},
' Name ': ' Laowu '
};


var O2 = {
' Name ': ' Laowu ',
' Age ': 18,
Info: {
' Author ': ' Lee ',
' Job ': [
' A ',
' B '
]
}
};

Console.log (O2O (O1,O2)); False


Type is inconsistent and the result is false

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.