两个对象比较内部数据的变化,并返回对应key数组

  • 比较两个对象内部相同key的数据是否发生变化,是则返回变化的key
  • 包含比较内部字符串 内部数组 内部对象
// obj1 obj2 比对的对象
// excludeKeys 排除的key
// 递归调用时的父级key
// defaultKey 递归判断时仅返回父级key / 父级.子级
export function compareObjects (obj1, obj2, excludeKeys = [], parentKey = '', defaultKey = true) {
  let keysWithDifferences = [];
  for (let key in obj1) {
    if (!excludeKeys.includes(key) && Array.isArray(obj1[key]) && Array.isArray(obj2[key])) {
      if (!arraysAreEqual(obj1[key], obj2[key], excludeKeys)) {
        if (defaultKey) {
          if (parentKey) {
            keysWithDifferences.push(parentKey);
            break
          } else {
            keysWithDifferences.push(key);
          }
        } else {
          keysWithDifferences.push(parentKey ? `${parentKey}.${key}` : key);
        }
      }
    } else if (!excludeKeys.includes(key) && Object.prototype.hasOwnProperty.call(obj1, key) && Object.prototype.hasOwnProperty.call(obj2, key)) {
      if (typeof obj1[key] === 'object' && typeof obj2[key] === 'object') {
        compareObjects(obj1[key], obj2[key], excludeKeys, key);
      } else if (obj1[key] != obj2[key]) {
        if (defaultKey) {
          if (parentKey) {
            keysWithDifferences.push(parentKey);
            break
          } else {
            keysWithDifferences.push(key);
          }
        } else {
          keysWithDifferences.push(parentKey ? `${parentKey}.${key}` : key);
        }
      }
    }
    // else {
    //   keysWithDifferences.push(parentKey ? `${parentKey}.${key}` : key);
    // }
  }
  for (let key in obj1) {
    if (!excludeKeys.includes(key) && !Object.prototype.hasOwnProperty.call(obj2, key)) {
      if (defaultKey) {
        if (parentKey) {
          keysWithDifferences.push(parentKey);
          break
        } else {
          keysWithDifferences.push(key);
        }
      } else {
        keysWithDifferences.push(parentKey ? `${parentKey}.${key}` : key);
      }
    }
  }
  return keysWithDifferences;
}
export function arraysAreEqual (arr1, arr2, excludeKeys) {
  for (let i = 0; i < arr1.length; i++) {
    if (!excludeKeys.includes(arr1[i]) && (typeof arr1[i] === 'object' && typeof arr2[i] === 'object')) {
      if (!deeplyEquals(arr1[i], arr2[i], excludeKeys)) {
        return false;
      }
    } else if (!excludeKeys.includes(arr1[i]) && Array.isArray(arr1[i]) && Array.isArray(arr2[i])) {
      if (!deeplyEquals(arr1[i], arr2[i], excludeKeys)) {
        return false;
      }
    }
    else if (!excludeKeys.includes(arr1[i]) && arr1[i] != arr2[i]) {
      return false;
    }
  }
  return true;
}
export function deeplyEquals (obj1, obj2, excludeKeys) {
  let keys1 = Object.keys(obj1);
  for (let key of keys1) {
    if (!excludeKeys.includes(key) && (typeof obj1[key] === 'object' && typeof obj2[key] === 'object')) {
      if (!deeplyEquals(obj1[key], obj2[key], excludeKeys)) {
        return false;
      }
    } else if (!excludeKeys.includes(key) && Array.isArray(obj1[key]) && Array.isArray(obj2[key])) {
      if (!arraysAreEqual(obj1[key], obj2[key], excludeKeys)) {
        return false;
      }
    } else if (!excludeKeys.includes(key) && obj1[key] != obj2[key]) {
      return false;
    }
  }
  return true;
}

项目中引入

import { compareObjects } from "@/utils/validate";

let result = compareObjects(params, params2, ["modifyMaterial", "modifyReason", "createType"])


// 示例用法
let object1 = {a: 1, b: {c: [2, 3], d: 4}, e: 5};
let object2 = {a: 1, b: {c: [2, 4], d: 4}, f: 6};
let result = compareObjects(object1, object2, [], true);
console.log(result); // 返回 ['b.c.1', 'e', 'f']

你可能感兴趣的:(javascript,前端)