Skip to content

Latest commit

 

History

History
31 lines (29 loc) · 921 Bytes

0507.md

File metadata and controls

31 lines (29 loc) · 921 Bytes

判断两个对象是否相等,这两个对象的值只能是数字或字符串

/**
 * @param {*} obj1 对象
 * @param {*} obj2 对象
 * @description 判断两个对象是否相等,这两个对象的值只能是数字或字符串
 */
export const objEqual = (obj1, obj2) => {
  const keysArr1 = Object.keys(obj1)
  const keysArr2 = Object.keys(obj2)
  if (keysArr1.length !== keysArr2.length) return false
  else if (keysArr1.length === 0 && keysArr2.length === 0) return true
  /* eslint-disable-next-line */
  else return !keysArr1.some(key => obj1[key] != obj2[key])
}

export const deepCopy = origin => {
  var target = Array.isArray(origin) ? [] : {};
  for (var prop in origin) {
    if (target[prop]) {
      continue;
    }
    var childOrigin = origin[prop];
    target[prop] = typeof childOrigin !== 'object'
      ? childOrigin
      : deepCopy(childOrigin);
  }
  return target;
}