10

Can someone show me how to return the new data when comparing something like this. using vanilla JavaScript.

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

compared to this

{
    "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\"}"
}

it should return only the differences.

{
    "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
    "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}
distante
  • 6,438
  • 6
  • 48
  • 90
aestheticode
  • 124
  • 1
  • 2
  • 13
  • 1
    Possible duplicate of [How to get the difference between two arrays in JavaScript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – Ari Seyhun Sep 12 '19 at 04:47
  • 1
    They are not multidimensional, they are flat objects. `[key: string]: string;` – Ari Seyhun Sep 12 '19 at 04:50
  • Possible duplicate of [Object comparison in JavaScript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – distante Sep 12 '19 at 05:06
  • Possible duplicate of [getting difference object from two objects using es6](https://stackoverflow.com/questions/57669696/getting-difference-object-from-two-objects-using-es6) – Nick Parsons Sep 15 '19 at 01:55
  • I give really nice answered here: https://stackoverflow.com/a/71173966/1919821 – pery mimon Feb 18 '22 at 14:18

5 Answers5

11

You can use Object.keys() and Array.includes() to do that.

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);
Nikhil
  • 6,493
  • 10
  • 31
  • 68
6

An object oriented approach using 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)

This approach will work with several objects and does not treat one object as the primary one for comparison.

Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
5

You could use Object.prototype.entries and Array.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);
fubar
  • 16,918
  • 4
  • 37
  • 43
2

You can simply iterate the second object & check if this key exist in first object. If not then in a new object add this key and its value.

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)
brk
  • 48,835
  • 10
  • 56
  • 78
1

You can use a procedure in which the order of the objects to compare does not matter

Code:

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))
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46