比较两个对象并删除之间的重复键

我正在试验的对象,我想要实现的是删除在object1find的键如果这些键存在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差异函数,但它不适用于对象,也不知道这是否是正确的function。

你能帮我得到var expectedResult对吗?

您可以使用迭代和recursion方法来将所需属性在新对象中进行通知。

 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; } 

这将是algorithm:

 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; } 

您可以创buildrecursion函数,它将返回使用for...in循环的新对象。

 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)) 

真正的工作一些recursion和一些使用纯函数的函数式编程。 (使用Node v7.7.1testing)

当“baddict”中存在相应的“叶子”时,“DoForAllNestedObjects”在“字典树上的每一片叶子上”应用一些函数“whattodo”。

 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);