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




object (4)

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

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

有人可以告诉我如何在比较这种情况时返回新数据。 使用香草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\"}"
}

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


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

码:

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


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

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)







object