Home>Article>Web Front-end> How to determine whether two objects are equal in es6

How to determine whether two objects are equal in es6

青灯夜游
青灯夜游 Original
2022-04-19 15:34:51 3786browse

In es6, the is() method of the Object object can be used to determine whether two objects are equal. This method detects whether the values of two variables are the same value and determines whether the reference addresses of the two objects are consistent. Syntax "Object.is(Object 1, Object 2)"; this method will return a Boolean value. If true is returned, it means that the two objects are equal.

How to determine whether two objects are equal in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Two Object type objects, even if they have the same attributes and values, are not considered equal when compared using == or ===. This is because they are compared by reference (location in memory), unlike primitive types which are compared by value.

var obj1 = { name: "xiaoming", sex : "male" } var obj2 = { name: "xiaoming", sex : "male" } console.log(obj1 === obj2); // false

But ES6 provides a method to determine whether two objects are equal. This method determines whether the reference addresses of the two objects are consistent.

Object.is(a,b)

Object.is() method determines whether the two values are equal. is the same value. The two values are equal if the following conditions are met:

  • are both undefined

  • are null

  • are all true or false

  • are all strings of the same length and the same characters are arranged in the same order

  • are all the same object ( means that each object has the same reference)

  • is a number and

    • is 0

    • are all -0

    • are all NaN

    • or are both non-zero and non-NaN and are the same value

Return value: A Boolean type to indicate whether the two parameters are the same value.

Example 1:

let obj1= { a: 1 } let obj2 = { a: 1 } console.log(Object.is(obj1, obj2)) // false let obj3 = obj1 console.log(Object.is(obj1, obj3)) // true console.log(Object.is(obj2, obj3)) // fals

How to determine whether two objects are equal in es6

Example 2:

Object.is('foo', 'foo') //true Object.is(window, window) //true Object.is([], []) //false Object.is(null, null) //true

Extended knowledge:

Object.is() method is different from == (en-US) operation. The == operator casts the variables on both sides (if they are not of the same type) before judging equality (the result of this behavior will judge "" == false as true), while Object.is does not cast the variables on both sides. value.

The Object.is() method is also different from the === (en-US) operation. The === operator (also includes the == operator) treats numbers -0 and 0 as equal, and Number.NaN and NaN as unequal.

[Related recommendations:javascript video Tutorialweb front-end

The above is the detailed content of How to determine whether two objects are equal in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn