Here is a JavaScript object serialized in JSon:
var source1 = '[{"vConfig":{"vType":"objectview","serverItemType":"TrackerObject"}}]';
And here is another JavaScript object, also serialized in JSon:
')
var source2 = '[{"vConfig":{"serverItemType":"TrackerObject","vType":"objectview"}}]';
They have the same structure, the same parameters, the same values in these parameters. By all indications, both in
source1
and in
source2
we have
the same thing .
But the JavaScript interpreter with us, of course, does not agree. And he quite reasonably believes that source1 and source2 are different lines. And if we deserialize them back, we get two objects, which are located at different memory addresses and ... are also not equal to each other.
And if you, in addition, work with Ext.js, generously generate your classes and do not forget about jSon, then you can reach a total eclipse. How to compare these huge sheets of information about controls that are collected in JSons? Or to sort out tree-like objects, where some other subfields have already been created in each field?
Only one way out - you need to find a way to compare not by value, not by cell in memory, but more flexibly.
The objects with the same fields containing the same values should be considered equal . From this point of view, our
source1
definitely equal to
source2
.
This is how the next bike was born -
JSonCmp . A simple, and very necessary function for comparing objects in JavaScript. Of course, I found many attempts to write one, but each of the implementations solved only part of the problem - in the end I put all the interesting ideas into one, adding a couple of my own along the way ...
Using it is easy - just connect
jsoncmp.js , and then call:
jSonCom(object1, object2);
If objects contain the same information, the function returns true. Else, false.
Ext.js users can use the same algorithm, but in a plug-in wrapper -
jsoncmp.ext.js . The code will look like this:
Ext.ux.util.Object(object1, object2);
Comparison rules are:
- null is null
- objects of different types are not equal
- variable by value (Float, Integer, Boolean) is compared by value
- lines are compared by value
- serialized json s are compared as deserialized objects
- functions are compared according to the source code converted to string
- jQuery objects are compared using the is standard function for this library.
- objects are compared by fields and their values. If the field value is also an object - it is compared according to the same principle.
- if, when traversing this tree, it turns out that the sheet refers to one of the already passed objects (this means that there is a cycle in the tree), the references to the objects will be compared. "
- arrays are compared both for matching elements and for their order
- if you set arraysAsSets = true in the search settings, then the arrays will be perceived as sets (set-s) and the sequence of elements will be ignored. Search settings are set in the third, optional parameter. In this way:
jSonCmp([ 1, 2, 3, 4, 5 ], [ 1, 2, 3, 5, 4 ], { arraysAsSets : true })); # - true
By default, arraysAsSets is set to false.
I hope that this is a small and probably imperfect function a little bit, but it will simplify your work.