jQuery / JavaScript JSON对象比较 [英] jQuery/JavaScript JSON object comparison

查看:197
本文介绍了jQuery / JavaScript JSON对象比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以比较两组json对象的区别?我所拥有的是一个通过jquery $ post()轮询JSON对象的脚本。我想要做的是获取刚刚轮询的对象并将其与存储的对象进行比较。如果从一个到另一个的任何变化将它们应用于存储的对象或替换它(以任何一种方式),但是从我正在使用的UI角度来看,通过找到它们之间的差异,无缝地将更改应用于JSON对象的用途。 2.我想这样做是因为我现在拥有它所以UI完全重新加载每个轮询,无论是否有变化,从UX角度来看基本上看起来像 * *。

Is it possible to compare 2 sets of json objects for a difference? What I have is a script thats polling for JSON object via jquery $post(). What I want to do is take the object that was just polled and compare it to a stored one. Where if there is any changes from one to the other apply them to the stored object or replace it (either way) but from the UI perspective I am working with seamlessly apply the changes to what the JSON object is for by finding the differences between the 2. I want to do this because right now I have it so the UI is completely reloading per poll regardless of change or not which basically looks like ** from a UX perspective.

我想如果我能找到两个对象之间的差异,我会启动一个函数,我会编辑特定于差异的UI。

I figure if I can find the differences if any between the 2 objects I would fire off a function that I would have edit the UI specific to the differences.

推荐答案


我想要做的是获取刚刚轮询的对象并将其与存储的对象进行比较。如果从一个到另一个的任何变化,将它们应用于存储的对象或替换它(无论哪种方式)

如果您对一个非常简单的它是否以任何方式改变了?是/否解决方案感到满意,如果它已经改变,您只需将新对象替换为新对象(根据您的问题部分) (我引用),然后您可以在解析它之前保存JSON响应,即将其保存为您的Web服务器发送它的字符串格式。然后当下一个响应进来时将新字符串与旧字符串进行比较。如果它们不同(或者如果它是第一个请求),则解析JSON并根据需要对其进行处理以进行显示。当然,这假设您的服务器端代码以一致的格式创建JSON字符串(而不是,例如,更改属性的顺序)。

If you would be happy with a really simple "Has it changed in any way? Yes/No" solution, where if it has changed you just replace the previous object with the new one (as per the part of your question that I quoted), then you could save the JSON response before you parse it, i.e., save it in the string format in which your web-server sends it. Then when the next response comes in compare the new string with the old string. If they are different (or if it is the first request) parse the JSON and process it for display as appropriate. Naturally this assumes that your server-side code is creating the JSON strings in a consistent format (and not, e.g., changing the order of the properties).

如果我们假设你已经得到(已解析)对象, isEqual(a,b)函数确实应该处理嵌套对象,属性是数组等。这可以递归完成,简单地返回true或false,但是 getDifferences(a,b)函数在报告嵌套对象中的差异时会变得混乱。考虑这个简单的例子:

If we assume you've already got (parsed) objects, an isEqual(a,b) function really should cope with nested objects, properties that are arrays, etc. This can be done recursively, and simply return true or false, but a getDifferences(a,b) function is going to get confusing in how it reports the differences within nested objects. Consider this simple example:

old: {"mum" : "Maria", "dad" : "Pierre", "kids" : ["Joe", "Mike", "Louisa"] }
new: {"mum" : "Julie", "dad" : "Pierre", "kids" : ["Joe", "Mary"] }

区别是 {妈妈:朱莉, 孩子们:[玛丽]} ? 妈妈已经改变,孩子的名单已经改变了,但是迈克变成了玛丽,或者迈克和路易莎都和玛丽一起消失了,或者...... ?也许它应该是kids:[Joe,Mary] 因为这是新值。你如何表明删除?这只是我头脑中的第一个例子,我不知道你将如何处理差异。它可能很快变得更糟:如果kids数组包含对象而不是字符串来表示整个家族树怎么办?如果新的妈妈属性是 [Maria,Julie] (允许继父等等)怎么办?

Is the difference {"mum" : "Julie", "kids" : ["Mary"]}? The "mum" has changed, and the list of "kids" has changed, but has "Mike" changed to "Mary", or are both "Mike" and "Louisa" gone with "Mary" being new, or...? Maybe it should be "kids": ["Joe","Mary"] because that's the new value. How do you indicate the deletions? That's just the first example off the top of my head where I don't know how you would want to handle the differences. It could quickly get worse: what if the "kids" array contained objects instead of strings to represent a whole family tree? What if the new "mum" property was ["Maria", "Julie"] (to allow for step-parents and so forth)?

如果对于您的特定数据,您知道您只有一维对象,那么您可以执行以下简单的操作:

If for your particular data you know you've only got one-dimensional objects then you can do something simple like the following:

function getDifferences(oldObj, newObj) {
   var diff = {};

   for (var k in oldObj) {
      if (!(k in newObj))
         diff[k] = undefined;  // property gone so explicitly set it undefined
      else if (oldObj[k] !== newObj[k])
         diff[k] = newObj[k];  // property in both but has changed
   }

   for (k in newObj) {
      if (!(k in oldObj))
         diff[k] = newObj[k]; // property is new
   }

   return diff;
}

对上面允许嵌套对象的最简单的改变就是假设如果属性是一个对象/数组,那么你只关心它是否有任何不同之处,而不是深入报告确切地报告了哪些子属性已经改变。如果是这样,只需采取上述函数并更改:

The simplest change to the above to allow for nested objects is to just assume that if a property is an object/array then you only care whether it is different in any way and not dig down to report exactly which "sub-properties" have changed. If so, simply take the above function and change:

else if (oldObj[k] !== newObj[k])

else if (!isEqual(oldObj[k],newObj[k]))

其中 isEqual()是浮动在网络上的众多比较函数之一或

Where isEqual() is one of the many comparison functions floating around the web or on StackOverflow.

(注意:我没有打扰 .hasOwnProperty()上面因为我假设作为JSON返回到Ajax请求的对象不会从原型链继承属性。类似地, isEqual()为此目的的函数不需要担心属性是函数,只需要担心JSON字符串中的有效内容。)

(Note: I haven't bothered with .hasOwnProperty() above because I assume that objects that were returned to an Ajax request as JSON will not be inheriting properties from a prototype chain. Similarly an isEqual() function for this purpose wouldn't need to worry about properties being functions, it only needs to worry about what is valid in a JSON string.)

这篇关于jQuery / JavaScript JSON对象比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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