[ACCEPTED]-Difference in JSON objects using Javascript/JQuery-json
The basic premise for iterating over objects 4 in JavaScript is like so
var whatever = {}; // object to iterate over
for ( var i in whatever )
{
if ( whatever.hasOwnProperty( i ) )
{
// i is the property/key name
// whatever[i] is the value at that property
}
}
Fixing up a checker 3 wouldn't be too hard. You'll need recursion. I'll 2 leave that as an exercise for you or another 1 SOer.
Maybe it's already answered enough, but 9 let me add my shameless plug :) A JSON (actually 8 any javascript object or array structure) diff 7 & patch library I open sourced at github:
https://github.com/benjamine/jsondiffpatch
it 6 generates diffs (also in JSON format, and 5 with a small footprint), which you can use 4 client (check the test page) & server 3 side, and if present, it uses http://code.google.com/p/google-diff-match-patch/ for long 2 strings automatically.
check the DEMO page to 1 see how it works.
You can iterate through the parent and child 1 object properties:
var diff = {};
for(var p in data){
if (old.hasOwnProperty(p) && typeof(data[p]) == 'object'){
diff[p] = {};
for(var i in data[p]){
if (old[p].hasOwnProperty(i)){
diff[p][i] = data[p][i] - old[p][i];
}
}
}
}
This did the trick for me when dealing with 4 a similar problem. It gets the differences 3 in second compared to first.
var first = originalObj;
var second = modifiedObj;
var diff = {};
var differ = function(first, second, result) {
var i = 0;
for (i in first) {
if (typeof first[i] == "object" && typeof second[i] == "object") {
result[i] = differ(first[i], second[i], {});
if (!result[i]) delete result[i];
} else if (first[i] != second[i]) {
result[i] = second[i];
}
}
return isEmpty(result) ? undefined : result;
}
differ(old_conf, new_conf, diff);
Code is a bit 2 of a special case, but you get the general 1 idea :P
You can use an object traversal module like 1 nervgh/object-traverse
to do this.
var result = {}
Object.traverse(old, function(node, value, key, path) {
var resultObject = result
for(var n=0; n<path.length-1; n++) {
resultObject = resultObject[path[n]]
}
resultObject[key] = value
});
Here is a solution using object-scan. Haven't tested 3 it, but this solution should be very fast 2 as we keep track of the two references
// const objectScan = require('object-scan');
const data = { eth0: { Tx: '4136675', Rx: '13232319' }, eth1: { Tx: '4', Rx: '0' }, lo: { Tx: '471290', Rx: '471290' } };
const old = { eth0: { Tx: '4136575', Rx: '13232219' }, eth1: { Tx: '4', Rx: '0' }, lo: { Tx: '471290', Rx: '471290' } };
const computeThroughput = (dataOld, dataNew) => objectScan(['**'], {
breakFn: ({ property, value, context: { stack, result } }) => {
if (property === undefined) {
return;
}
const stackRef = stack[stack.length - 1][property];
stack.push(stackRef);
const resultRefParent = result[result.length - 1];
if (!(property in resultRefParent)) {
if (typeof stackRef === 'string') {
resultRefParent[property] = String(Number.parseFloat(stackRef) - Number.parseFloat(value));
} else {
resultRefParent[property] = {};
}
}
const resultRef = resultRefParent[property];
result.push(resultRef);
},
filterFn: ({ context: { stack, result } }) => {
stack.pop();
result.pop();
}
})(dataOld, {
stack: [dataNew],
result: [{}]
}).result[0];
console.log(computeThroughput(old, data));
/* => {
lo: { Rx: '0', Tx: '0' },
eth1: { Rx: '0', Tx: '0' },
eth0: { Rx: '100', Tx: '100' }
} */
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>
Disclaimer: I'm 1 the author of object-scan
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.