输入两个带判断对象,输出所有value不同的key

  1. const findDiffKey = function (obj1, obj2) {
  2. const arr = []
  3. for (const key in obj1) {
  4. if (obj1.hasOwnProperty(key) && obj2.hasOwnProperty(key)) {
  5. if (obj1[key] === obj2[key] && JSON.stringify(obj1[key]) !== JSON.stringify(obj2[key])) {
  6. arr.push({key: key, value: obj1[key], type: 'obj'})
  7. } else if (obj1[key] !== obj2[key] && JSON.stringify(obj1[key]) !== JSON.stringify(obj2[key])) {
  8. arr.push({key: key, value: obj1[key]})
  9. }
  10. }
  11. }
  12. return arr
  13. }