Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript object equals

function isEqual(obj1, obj2) {
  const props1 = Object.getOwnPropertyNames(obj1);
  const props2 = Object.getOwnPropertyNames(obj2);
  if (props1.length != props2.length) {
      return false;
  }
  for (let i = 0; i < props1.length; i++) {
      let val1 = obj1[props1[i]];
      let val2 = obj2[props1[i]];
      let isObjects = isObject(val1) && isObject(val2);
      if (isObjects && !isEqual(val1, val2) || !isObjects && val1 !== val2) {
          return false;
      }
  }
  return true;
}
function isObject(object) {
  return object != null && typeof object === 'object';
}
 
PREVIOUS NEXT
Tagged: #javascript #object #equals
ADD COMMENT
Topic
Name
1+1 =