How to compare objects in JavaScript

Source: Internet
Author: User

This article will introduce how to compare objects in JavaScript.

In JavaScript, it is a little troublesome to compare the members of two objects. If it is only the first layer, it is easy, but the attributes in the sub-object may be an object, so it can only be recursive.

The Code is as follows: Copy code

// Deduplication
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;
};

Usage


Var o1 = {
Age: 18,
Info :{
'Author': 'lil ',
'Job ':[
'A', 'B'
]
},
'Name': 'laowu'
};


Var o2 = {
'Name': 'laowu ',
'Age': 18,
Info :{
'Author': 'lil ',
'Job ':[
'A ',
'B'
]
}
};

Console. log (o2o (o1, o2); // true

Replace the age of o1 with the 18 of the string

Var o1 = {
Age: "18 ",
Info :{
'Author': 'lil ',
'Job ':[
'A', 'B'
]
},
'Name': 'laowu'
};


Var o2 = {
'Name': 'laowu ',
'Age': 18,
Info :{
'Author': 'lil ',
'Job ':[
'A ',
'B'
]
}
};

Console. log (o2o (o1, o2); // false

The type is inconsistent and the result is false.


Common sense of equality of Variables

Determining whether two variables are equal is a very important operation in programming. When processing the original value, this operation is quite simple, but the task is a little complicated when it involves objects. ECMAScript provides two sets of equality operators: equal signs and non-equal signs are used to process original values, and full and non-full signs are used to process objects. Equal sign and non-equal sign
In ECMAScript, the equal sign is expressed by the equal sign (=). if and only when the two arithmetic numbers are equal, true is returned. The non-equal sign is added by the exclamation point (! =) Indicates that it returns true if and only if the number of two operations is not equal. To determine whether two arithmetic operations are equal, both operators perform type conversion. The type conversion rule is as follows: if a number is a Boolean value, convert it to a numeric value before checking equality. Convert false to 0, and true to 1.
If one operation is a string and the other is a number, convert the string to a number before checking for equality.
If one operation is an object and the other is a string, try to convert the object to a string before checking equality.
If one operation is an object and the other is a number, convert the object to a number before checking for equality.
During comparison, this operator also complies with the following rules: the values of null and undefined are equal.
When equality is checked, null and undefined cannot be converted into other values.
If the number of operations is NaN, the equal sign returns false, and the non-equal sign returns true.
If both arithmetic operations are objects, the referenced values are compared. If the number of two operations points to the same object, the equal sign returns true; otherwise, the number of two operations varies.
Important: Even if both numbers are NaN, the equal sign still returns false, because according to the rule, NaN is not equal to NaN. The following table lists some special cases and their results: expression values.
Null = undefined true
"NaN" = NaN false
5 = NaN false
NaN = NaN false
NaN! = NaN true
False = 0 true
True = 1 true
True = 2 false
Undefined = 0 false
Null = 0 false
"5" = 5 true full equal sign and non full equal sign
Similar operators of equal signs and non-equal signs are full and non-full. The two operators are the same as equal signs and non-equal signs, but they do not perform type conversion before checking equality. The full equal sign is represented by three equal signs (=). true is returned only when the number of type conversion operations is equal. Example: var sNum = "66 ";
Var iNum = 66;
Alert (sNum = iNum); // outputs "true"
Alert (sNum === iNum); // output "false"
In this Code, the first alert compares the string "66" and number 66 with the equal sign, and outputs "true ". As mentioned above, this is because the string "66" will be converted to a number 66 before being compared with another number 66. The second alert compares the string and number with the equal sign without type conversion. Of course, the string is not equal to the number, so the output is "false ". The non-equal sign is added by the exclamation point and two equal signs (! =) Indicates that true is returned only when the number of type conversion operations is not equal. Example: var sNum = "66 ";
Var iNum = 66;
Alert (sNum! = INum); // output "false"
Alert (sNum! = INum); // output "true"
Here, the first alert uses a non-equal sign to convert the string "66" to the number 66, so that it is equal to the number 66 of the second operation. Therefore, the calculation result is "false" because the two computations are equal. The second alert uses a non-full equal sign. Is the operation "sNum" different from "iNum? The answer to this question is: Yes (true), because sNum is a string, and iNum is a number, they are certainly different.

If the two values have different types, false is returned.
Returns true if both values are of the number type and the values are the same.
Returns true if both values are stirng and the values have the same String content.
Returns true if both values are true or false.
Returns true if both values point to the same Object, Arraya, or function.
Returns true if both values are null or undefined.
= Operator:
If the two values have the same type, the = comparison is performed, and the = comparison value is returned.
If the two values do not have the same type, true may be returned.
Returns true if one value is null and the other value is undefined.
If one value is string and the other is number, the string is converted to number before comparison.
If a value is true, it is converted to 1 and then compared, and false is converted to 0.
If one value is an Object and the other value is a number or string, the Object is converted to the original type using valueOf () or toString () and then compared.

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.