JavaScript Comparison object Equality method detailed

Source: Internet
Author: User
Tags numeric value

It's a bit of a hassle to compare the members of two objects in JavaScript, but if it's just the first level comparison, it's easy, but the attributes in the child object may be an object, so you can only recursively

The code is as follows Copy 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;
};

How to use


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)); 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


Add variable equality common sense

Determining whether two variables are equal is a very important operation in programming. This is a fairly simple operation when dealing with raw values, but the task is slightly more complex when it comes to objects. ECMAScript provides two sets of equality operators: the Equals and the non-equals are used to process the original values, and the full equals and the non-full equals are used to process the object. Equals and non-equals
In ECMAScript, the equal sign is represented by a double equal sign (= =) that returns True if and only if two operands are equal. A non equal sign is represented by an exclamation point plus an equal sign (!=) that returns true if and only if the two operands are not equal. To determine whether two operands are equal, both operators perform type conversions. The rules for performing type conversions are as follows: If an operand is a Boolean value, convert it to a numeric value before checking for equality. False converts to 0,true 1.
If one of the operands is a string and the other is a number, try converting the string to a number before checking for equality.
If one of the operands is an object, and the other is a string, try to convert the object to a string before checking for equality.
If one of the operands is an object, and the other is a number, try converting the object to a number before checking for equality.
When compared, the operator also adheres to the following rules: value null and undefined equal.
You cannot convert null and undefined to other values when checking for equality.
If one of the operands is NaN, the equal sign returns false, and a non-equal sign returns TRUE.
If the two operands are objects, they are compared to their reference values. If two operands point to the same object, the equal sign returns TRUE, otherwise the two operands are unequal.
Important: Even if the two numbers are Nan, the equals sign returns false because, according to the rule, Nan is not equal to Nan. The following table lists some of the 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 equals and non-full equals
Equal and non-equal equals are all equals and not equals. The two operators do the same as equals and non equals, except that they do not perform type conversions until the equality is checked. The full equals sign (= = =) is represented by three equals (= =) and returns true only if no type conversion shipping arithmetic is equal. For example: var snum = "66";
var inum = 66;
Alert (Snum = = Inum); Output "true"
Alert (Snum = = Inum); Output "false"
In this code, the first alert uses the equal sign to compare the string "66" with the number 66, and the output "true". As mentioned earlier, this is because the string "66" will be converted to the number 66 and then compared to another number 66. The second alert uses the full equals sign to compare strings and numbers without type conversions, and of course, the string is not equal to a number, so the output is "false." A non-full equal sign, represented by an exclamation point plus two equal signs (!==), returns true only if no type conversion operands are equal. For 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, making it equal to 66 of the second operand. Therefore, the result of the calculation is "false" because the two operands are equal. A non-full equal sign used by the second alert. The operation is asked: "Snum" and "Inum" different? The answer to this question is: Yes (true), because Snum is a string, and Inum is a number, of course they are different.

Returns False if two value types are different
Returns true if two values are the number type and the values are the same
Returns true if two values are stirng and the string content of two values is the same
Returns true if all two values are true or false
Returns true if two values are pointing to the same Object,arraya or function.
Returns true if all two values are null or undefined.
= = Operator:
If two values have the same type, a = = = comparison is made and the comparison value of = = = is returned
It is possible to return true if two values do not have the same type
Returns true if one value is null and another value is undefined
If one value is a string and the other is number, the string is converted to number and then compared
If a value is true, it is converted to 1 and then the false is converted to 0
If one value is object and the other is number or string, the object is converted using valueof () or ToString () to the original type for comparison

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.