JavaScript-返回两个对象之间的差异?

时间:2019-09-12 04:45:46

标签: javascript object

有人可以告诉我如何在比较这种情况时返回新数据。使用香草JavaScript。

{
    "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
    "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
}

与此相比

{
    "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
    "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
    "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
    "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}

它应该只返回差异。

{
    "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
    "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}

5 个答案:

答案 0 :(得分:7)

您可以使用Object.keys()Array.includes()来做到这一点。

var data = {
  "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
  "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
  "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
  "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
};

var obj1 = {
  "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
  "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
};

var result = {};
var keys = Object.keys(obj1);

for (var key in data) {
 if (!keys.includes(key)) {
  result[key] = data[key];
 }
}

console.log(result);

答案 1 :(得分:3)

您可以使用Object.prototype.entriesArray.prototype.reduce

const a = {
    "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
    "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
};
const b = {
    "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
    "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
    "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
    "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
};

const c = Object.entries(b).reduce((c, [k, v]) => Object.assign(c, a[k] ? {} : { [k]: v }), {});

console.log(c);

答案 2 :(得分:2)

使用reduce的面向对象方法。

const obj1 = {
  '48': '{"sid":"48","name":"title 1"}',
  '77': '{"sid":"77","name":"The blahblah title"}',
}

const obj2 = {
  '48': '{"sid":"48","name":"title 1"}',
  '77': '{"sid":"77","name":"The blahblah title"}',
  '83': '{"sid":"83","name":"The blahblah derp"}',
  '87': '{"sid":"87","name":"The derpy title 4"}',
}

const combinedObject = { ...obj1, ...obj2 }

const diff = Object.entries(combinedObject).reduce((acc, [key, value]) => {
  if (
    !Object.values(obj1).includes(value) ||
    !Object.values(obj2).includes(value)
  )
    acc[key] = value

  return acc
}, {})

console.log(diff)

这种方法将适用于多个对象,并且不会将一个对象作为主要对象进行比较。

答案 3 :(得分:2)

您可以简单地迭代第二个对象并检查该键是否存在于第一个对象中。如果不是,则在一个新对象中添加此键及其值。

let data1 = {
  "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
  "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
}
let data2 = {
  "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
  "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
  "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
  "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}

let newObj = {};

for (let keys in data2) {
  if (!data1[keys]) {
    newObj[keys] = data2[keys]
  }
};

console.log(newObj)

答案 4 :(得分:1)

您可以使用一个过程,其中要比较的对象的顺序无关紧要

代码:

const obj1 = {
  "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
  "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
}
const obj2 = {
  "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
  "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
  "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
  "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}

const getDiffObj = (o1, o2) =>  Object.keys(o1)
    .filter(k => !Object.keys(o2).includes(k))
    .concat(Object.keys(o2).filter(k => !Object.keys(o1).includes(k)))
    .map(k => o1[k] || o2[k])

console.log(getDiffObj(obj1, obj2))
console.log(getDiffObj(obj2, obj1))