比较 2 个对象并删除它们之间的重复键

Compare 2 objects and remove repeating keys between

我正在对对象进行试验,我想要实现的是删除 object1 中找到的键(如果这些键存在于 object2.

中)

示例如下:

var original = {
    a: 1, 
    b: 2, 
    c: 3,
    e: {
        tester: 0,
        combination: {
            0: 1
        }
    },
    0: {
        test: "0",
        2: "hello"
    }
};

var badKeys = {
    a: 1,
    b: 2,
    0: {
        test: "0",
    }
}


var expectedResult = {
    c: 3,
    e: {
        tester: 0, 
        combination: {
            0: 1
        }
    },
    0: {
        2: "hello"
    }

}

我试过使用underscore差异函数,但它对对象不起作用,也不确定这是否是正确的函数。

你能帮我把 var expectedResult 弄对吗?

您可以使用 for...in 循环创建将 return 新对象的递归函数。

var original = {"0":{"2":"hello","test":"0"},"a":1,"b":2,"c":3,"e":{"tester":0,"combination":{"0":1}}}
var badKeys = {"0":{"test":"0"},"a":1,"b":2}

function remove(o1, o2) {
  var result = {}
  for (var i in o1) {
    if (!o2[i]) result[i] = o1[i]
    else if (o2[i]) {
      if (typeof o1[i] == 'object' && typeof o2[i] == 'object') {
        result[i] = Object.assign(result[i] || {}, remove(o1[i], o2[i]))
      } else if (o1[i] != o2[i]) result[i] = o1[i]
    }
  }
  return result
}


console.log(remove(original, badKeys))

您可以使用迭代和递归方法在新对象中获取所需的属性。

function deleteKeys(good, bad, result) {
    Object.keys(good).forEach(function (key) {
        if (bad[key] && typeof bad[key] === 'object') {
            result[key] = {};
            deleteKeys(good[key], bad[key], result[key]);
            return;
        }
        if (!(key in bad) || good[key] !== bad[key]) {
            result[key] = good[key];
        }
    });
}

var original = { a: 1, b: 2, c: 3, e: { tester: 0, combination: { 0: 1 } }, 0: { test: "0", 2: "hello", another: { a: { B: 2, C: { a: 3 } }, b: 2 } } },
    badKeys = { a: 1, b: 2, 0: { test: "0", random: 2, another: { a: 1 } } },
    result = {};

deleteKeys(original, badKeys, result);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

这将是算法:

function removeDifferences (original, removeKeys) {
  // Get keys of to be deleted properties.
  var keys = Object.keys(removeKeys);

  // Iterate all properties on removeKeys.
  for (key of keys) {
    // Check if property exists on original.
    if (typeof original[key] !== undefined) {
      // If the property is an object, call same function to remove properties.
      if (typeof removeKeys[key] === 'object') {
        removeDifferences(original[key], removeKeys[key]);
      } else {
        delete original[key];
      }
    }
  }
  return original;
}

适用于您的情况:

/* Your data. */

var original = {
  a: 1,
  b: 2,
  c: 3,
  e: {
    tester: 0,
    combination: {
      0: 1
    }
  },
  0: {
    test: "0",
    2: "hello"
  }
};

var badKeys = {
  a: 1,
  b: 2,
  0: {
    test: "0",
  }
};

var expectedResult = {
  c: 3,
  e: {
    tester: 0,
    combination: {
      0: 1
    }
  },
  0: {
    2: "hello"
  }

};

/* Function */

function removeDifferences(original, removeKeys) {
  // Get keys of to be deleted properties.
  var keys = Object.keys(removeKeys);

  // Iterate all properties on removeKeys.
  for (key of keys) {
    // Check if property exists on original.
    if (typeof original[key] !== undefined) {
      // If the property is an object, call same function to remove properties.
      if (typeof removeKeys[key] === 'object') {
        removeDifferences(original[key], removeKeys[key]);
      } else {
        delete original[key];
      }
    }
  }
  return original;
}

/* Application */
var output = removeDifferences(original, badKeys);
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

真正的工作是一些递归和一些使用纯函数的函数式编程。 (使用 Node v7.7.1 测试)

"DoForAllNestedObjects" 用于在 "every leaf on the dictionary tree" 上应用某些函数 "whattodo",当 baddict 中有相应的 "leaf" 时。

let DoForAllNestedValues = (dict, baddict, whattodo) => {
    for (let key in dict) {
        if (typeof (dict[key]) === 'object' && typeof (baddict[key]) === 'object')
            DoForAllNestedValues(dict[key], baddict[key], whattodo);
        else
            if (baddict[key])
                whattodo(dict, key);
    }
}

DoForAllNestedValues(original, badKeys, (obj, val) => delete obj[val]);
console.log(original);