[javascript] How to determine equality for two JavaScript objects?

_x000D_
_x000D_
function isDeepEqual(obj1, obj2, testPrototypes = false) {_x000D_
  if (obj1 === obj2) {_x000D_
    return true_x000D_
  }_x000D_
_x000D_
  if (typeof obj1 === "function" && typeof obj2 === "function") {_x000D_
    return obj1.toString() === obj2.toString()_x000D_
  }_x000D_
_x000D_
  if (obj1 instanceof Date && obj2 instanceof Date) {_x000D_
    return obj1.getTime() === obj2.getTime()_x000D_
  }_x000D_
_x000D_
  if (_x000D_
    Object.prototype.toString.call(obj1) !==_x000D_
      Object.prototype.toString.call(obj2) ||_x000D_
    typeof obj1 !== "object"_x000D_
  ) {_x000D_
    return false_x000D_
  }_x000D_
_x000D_
  const prototypesAreEqual = testPrototypes_x000D_
    ? isDeepEqual(_x000D_
        Object.getPrototypeOf(obj1),_x000D_
        Object.getPrototypeOf(obj2),_x000D_
        true_x000D_
      )_x000D_
    : true_x000D_
_x000D_
  const obj1Props = Object.getOwnPropertyNames(obj1)_x000D_
  const obj2Props = Object.getOwnPropertyNames(obj2)_x000D_
_x000D_
  return (_x000D_
    obj1Props.length === obj2Props.length &&_x000D_
    prototypesAreEqual &&_x000D_
    obj1Props.every(prop => isDeepEqual(obj1[prop], obj2[prop]))_x000D_
  )_x000D_
}_x000D_
_x000D_
console.log(isDeepEqual({key: 'one'}, {key: 'first'}))_x000D_
console.log(isDeepEqual({key: 'one'}, {key: 'one'}))
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor

Examples related to equals

this in equals method Why do we have to override the equals() method in Java? Compare two objects with .equals() and == operator Check if bash variable equals 0 Setting equal heights for div's with jQuery Java, how to compare Strings with String Arrays How can I express that two values are not equal to eachother? How to override equals method in Java How do you say not equal to in Ruby? Getting an element from a Set

Examples related to hashcode

Hashing with SHA1 Algorithm in C# HashMaps and Null values? How to create a HashMap with two keys (Key-Pair, Value)? What is hashCode used for? Is it unique? How does a Java HashMap handle different objects with the same hash code? Hashcode and Equals for Hashset What is the use of hashCode in Java? Good Hash Function for Strings Why do I need to override the equals and hashCode methods in Java? Memory address of variables in Java