简体   繁体   English

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

[英]JavaScript - Return differences between two Objects?

Can someone show me how to return the new data when comparing something like this. 有人可以告诉我在比较这样的事情时如何返回新数据。 using vanilla JavaScript. 使用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\"}"
}

You can use Object.keys() and Array.includes() to do that. 您可以使用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); 

You could use Object.prototype.entries and Array.prototype.reduce . 您可以使用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); 

An object oriented approach using reduce . 一种面向对象的方法,使用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. 此方法适用于多个对象,并不会将一个对象视为主要对象。

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) 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM