Javascript JSON 比较/差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10357830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript JSON comparison/diff?
提问by Rolando
Say I have the following 2 json objects:
假设我有以下 2 个 json 对象:
JSON A:
{
"Field A":"1",
"Field B":"2",
"Field D":"Something",
"Field E":"6"
}
JSON B:
{
"Field A":"1",
"Field B":"2",
"Field C":"3",
"Field D":"Different"
}
Sample Function: function (jsonstringA, jsonstringB)
示例函数:函数 (jsonstringA, jsonstringB)
Example (If JSON A and JSON B used as parameters):
示例(如果 JSON A 和 JSON B 用作参数):
Returns a new JSON object containing:
返回一个新的 JSON 对象,其中包含:
{
"Field C":"3", // because function sees jsonstringB had no "Field C"
"Field D": "Different" // sees jsonstringB had a different value for "Field D"
}
Note that it is using jsonstringA as the base of the comparison, so the function returns only the fields missing and values of jsonStringB. That is why "Field E" and its value is not returned.
请注意,它使用 jsonstringA 作为比较的基础,因此该函数仅返回缺少的字段和 jsonStringB 的值。这就是为什么不返回“字段 E”及其值的原因。
What is the best way if possible to come up with a function that returns a json object containing values that have changed?
如果可能,想出一个返回包含已更改值的 json 对象的函数的最佳方法是什么?
WHAT I HAVE TRIED: I have tried doing a comparison by manually specifying the fields that I am trying to check for, but I would like to have something that requires me to not hardcode the "Fields" as it is very inefficient and everytime I add a new field to JSON B, I have to hardcode in the field I am looking for... that is why I am looking for something less of a pain.
我尝试过的方法:我尝试通过手动指定要检查的字段来进行比较,但我希望有一些东西要求我不要对“字段”进行硬编码,因为它非常低效并且每次添加时JSON B 的新字段,我必须在我正在寻找的字段中进行硬编码......这就是为什么我正在寻找不那么痛苦的东西。
回答by Peter Olson
It's not too hard to create a function like this. Just loop through each field in the second object, and if it's not present in the first or the value is different than the first, put that field in the return object.
创建这样的函数并不难。只需循环遍历第二个对象中的每个字段,如果第一个对象中不存在该字段或值与第一个不同,则将该字段放入返回对象中。
var compareJSON = function(obj1, obj2) {
var ret = {};
for(var i in obj2) {
if(!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
ret[i] = obj2[i];
}
}
return ret;
};
You can see it in action on this demo page.
您可以在此演示页面上看到它的运行情况。
回答by Hüseyin BABAL
回答by Steve Black
The function, just what i was after is also useful for non - JSON object comparison
该函数,正是我所追求的,对于非 JSON 对象比较也很有用
Also extended it for deep nested objects.
还为深层嵌套对象扩展了它。
isEmpty
can be done in many ways. see Is object empty?
可以通过多种方式完成。请参阅 对象是否为空?
var compareObj = function(obj1, obj2) {
var ret = {},rett;
for(var i in obj2) {
rett = {};
if (typeof obj2[i] === 'object'){
rett = compareObj (obj1[i], obj2[i]) ;
if (!isEmpty(rett) ){
ret[i]= rett
}
}else{
if(!obj1 || !obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
ret[i] = obj2[i];
}
}
}
return ret;
};
回答by LogaKrishnan
In the Peter Olson answer, it will not check for array
values, to overcome from that issue, I have modified the solution like this.
在 Peter Olson 的回答中,它不会检查array
值,为了克服这个问题,我修改了这样的解决方案。
var compareJSON = function(obj1, obj2) {
var ret = {};
for(var i in obj2) {
if(!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
if(!Array.isArray(obj2[i]) || !(JSON.stringify(obj2[i]) == JSON.stringify(obj1[i]))){
ret[i] = obj2[i];
}
}
}
return ret;
};
回答by user1717344
This solution does not work if obj2's properties are equal to obj1's, but obj1 has more properties.
如果 obj2 的属性等于 obj1 的属性,则此解决方案不起作用,但 obj1 具有更多属性。