如何仅获得“更改"?来自两个JSON对象的值 [英] how to get only the "changed" values from two JSON objects

查看:85
本文介绍了如何仅获得“更改"?来自两个JSON对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个涉及更复杂的比较方式的问题,因此它不是重复的

我创建了一个 JqTree ,当用户更改其树形结构时,应该比较旧的"JSON"和新的" JSON结构,并且应该仅显示已更改的JSON的值.

I've created a JqTree, that when the user changes its tree structure, both "old" JSON and "new" JSON structures should be compared, and it should be shown only the values of the JSON that has been changed.

例如:

[{"name":"node1","id":1,"is_open":true,"children":
    [
      {"name":"child1","id":2},
      {"name":"child2","id":3}
    ]
}]

客户将child1放置在

[{"name":"node1","id":1,"is_open":true,"children":
  [
    {"name":"child2","id":3},
    {"name":"child1","id":2}
  ]
}]

我只想比较它们,并检查更改了哪些值,并用alert显示它们,在这种情况下为:

I just would like to compare them and check which values were changed and show them with an alert, which, in this case, would be:

{名称":"child2","id":3},
{"name":"child1","id":2}

{"name":"child2","id":3},
{"name":"child1","id":2}

到目前为止,我有一个比较小的代码:

JSON.stringify(object1)=== JSON.stringify(object2) ; //我知道它不太可靠.

JSON.stringify(object1) === JSON.stringify(object2); //I know it's not too reliable.

但是我正在寻找一种检查差异"并将其从JSON中提取出来的东西.

But I'm looking for something that checks the "difference" and extracts it from the JSON.

谢谢.

推荐答案

在这里: http://jsfiddle.net/musicin3d/cf5ddod1/3/

经过精简的版本,为您带来无与伦比的愉悦感:

Edited down version for your no-clicking pleasure:

// Call this function.
// The others are helpers for this one.
function getDiff(a, b){
    var diff = (isArray(a) ? [] : {});
    recursiveDiff(a, b, diff);
    return diff;
}

function recursiveDiff(a, b, node){
    var checked = [];

    for(var prop in a){
        if(typeof b[prop] == 'undefined'){
            addNode(prop, '[[removed]]', node);
        }
        else if(JSON.stringify(a[prop]) != JSON.stringify(b[prop])){
            // if value
            if(typeof b[prop] != 'object' || b[prop] == null){
                addNode(prop, b[prop], node);
            }
            else {
                // if array
                if(isArray(b[prop])){
                   addNode(prop, [], node);
                   recursiveDiff(a[prop], b[prop], node[prop]);
                }
                // if object
                else {
                    addNode(prop, {}, node);
                    recursiveDiff(a[prop], b[prop], node[prop]);
                }
            }
        }
    }
}

function addNode(prop, value, parent){
        parent[prop] = value;
}

function isArray(obj){
    return (Object.prototype.toString.call(obj) === '[object Array]');
}

有关更多详细信息,请参见上面的链接.有一条评论解释了我的一些假设.

See the link above for more details. There is a comment that explains some of my assumptions.

这是如何使用递归解决问题的示例.如果您不熟悉递归,建议您阅读一些内容.这是关于它的SO文章: 什么是递归,什么时候应该使用它?

This is an example of how to use recursion to solve your problem. If you're not familiar with recursion, I suggest you do some reading. Here's a SO article about it: What is recursion and when should I use it?

值得注意的是:
像我一样使用JSON.stringify并不是一个好主意.作为程序员,这对我来说很方便,因为我的程序可以向前看",看看每条路径上是否都有变化,但这是有代价的.我已经遍历树结构做我的工作,并且JSON.stringify 遍历我每次调用它发送的每个对象的树结构.在计算机科学中,我们称这为O(n!)的最坏情况,这种情况不太正式地称为非常慢".更好的设计将仅遍历整个树并跟踪其到达位置的方式.当到达死胡同(称为叶子"节点)时,它将使用此知识一次将必要的数据结构添加到diff变量中,这意味着我们的程序将必须遍历整个数据结构,但我们的代码将是唯一要做的事情,因此每个节点只能处理一次.

Of note:
Using JSON.stringify like I did is not a good idea. It's convenient for me as the programmer because my program can "look ahead" to see if there's a change down each path, but that comes at a cost. I'm already traversing the tree structure to do my work, and JSON.stringify also traverses the tree structure of every object I send every time I call it. In computer science we call this a worst case scenario of O(n!), less formally referred to as "very slow." A better design would just traverse the entire tree and keep track of how it got to where it was. When it came to a dead end (called a "leaf" node"), it would use this knowledge to add the necessary data structure to the diff variable all at once. This would mean our program would have to traverse the entire data structure, but our code would be the only thing doing it. So each node would be processed only once.

仍然,这应该使您了解其他人的建议.

Still, this should give you an idea of what others were suggesting.

这篇关于如何仅获得“更改"?来自两个JSON对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆