How to test if two objects are the same with JavaScript?

前端 未结 5 2033
慢半拍i
慢半拍i 2021-02-15 17:35

I need a function:

function isSame(a, b){
} 

In which, if a and b are the same, it returns true.
, I tried return a === b, b

5条回答
  •  旧时难觅i
    2021-02-15 17:55

    If anyone reading this answer is using Angular.js, you can use angular.equals(obj1,obj2);

    According to the docs:

    Determines if two objects or two values are equivalent. Supports value types, regular expressions, arrays and objects. Two objects or values are considered equivalent if at least one of the following is true:

    • Both objects or values pass === comparison.
    • Both objects or values are of the same type and all of their properties are equal by comparing them with angular.equals.
    • Both values are NaN. (In JavaScript, NaN == NaN => false. But we consider two NaN as equal).
    • Both values represent the same regular expression (In JavaScript, /abc/ == /abc/ => false. But we consider two regular expressions as equal when their textual representation matches).
    • During a property comparison, properties of function type and properties with names that begin with $ are ignored.

    Scope and DOMWindow objects are being compared only by identify (===).

提交回复
热议问题