判断对象里属性名与值是否相同

/**
* @des 判断数组属性是否相同
* @param {Array} a1
* @param {Array} a2
* @return {Boolean}
*/
function arrayEqual(a1,a2){ if(a1.length !== a2.length) return false
  for(let key of a1){
    if(!a2.includes(key)) return false
  }
 return true
}

/**
*des 判断对象属性与属性值是否相同
*param {Object} o1
*param {Object} o2
*@return {Boolean}
*/
function objEqual(o1,o2){
  let keysO1 = Object.getOwnPropertyNames(o1)
  let keysO2 = Object.getOwnPropertyNames(o2)
  if(keysO1.length !== keysO2.length) return false
  for(let item of keysO1){
    let keyO1 = o1[item]
    let keyO2 = o2[item]
    if(!o2.hasOwnProperty(item)) return false
    if(Object.prototype.toString.call(keyO1) === '[object Object]'){
       if(!this.objEqual(keyO1,keyO2)) return false
    }else if(Obejct.prototype.toString.call(keyO1) === '[object Array]'){
      if(!arrayEqual(keyO1,keyO2)) return false
    }else if(keyO1 !== keyO2){
      return false
    }
  }
  return true
}

 

上一篇:2021-04-12


下一篇:21.9.18 Compartor、Comparable