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

时间:2017-03-11 11:57:28

标签: javascript node.js underscore.js

我正在试验对象,我想要实现的是删除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吗?

4 个答案:

答案 0 :(得分:1)

您可以使用迭代和递归方法在新对象中对所需属性进行geeting。

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

答案 1 :(得分:0)

您可以使用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))

答案 2 :(得分: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; }




答案 3 :(得分:0)

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

" DoForAllNestedObjects"用于应用某些功能" whattodo" on"词典树上的每一片叶子"当有相应的" leaf"在baddict。

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