compare 2 json对象返回一个包含所有差异的数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了compare 2 json对象返回一个包含所有差异的数组相关的知识,希望对你有一定的参考价值。
pass in 2 json objects returns an array of all differences included those nested in children._Q used internally to pass through to self, do not pass in _Q
var compare = function (obj1, obj2, _Q){ _Q = (_Q == undefined)? new Array : _Q; function size(obj) { var size = 0; for (var keyName in obj){ if(keyName != null) size++; } return size; }; if (size(obj1) != size(obj2)) { //console.log('JSON compare - size not equal > '+keyName) }; var newO2 = jQuery.extend(true, {}, obj2); for(var keyName in obj1){ var value1 = obj1[keyName], value2 = obj2[keyName]; delete newO2[keyName]; if (typeof value1 != typeof value2 && value2 == undefined) { _Q.push(['missing', keyName, value1, value2, obj1]) }else if (typeof value1 != typeof value2) { _Q.push(['diffType', keyName, value1, value2, obj1]) }else{ // For jQuery objects: if (value1 && value1.length && (value1[0] !== undefined && value1[0].tagName)) { if (!value2 || value2.length != value1.length || !value2[0].tagName || value2[0].tagName != value1[0].tagName) { _Q.push(['diffJqueryObj', keyName, value1, value2, obj1]) } }else if(value1 && value1.length && (value1.tagName !== value2.tagName)){ }else if (typeof value1 == 'function' || typeof value2 == 'function') { _Q.push(['function', keyName, value1, value2, obj1]) }else if(typeof value1 == 'object'){ var equal = Arcadia.Utility.CompareJson(value1, value2, _Q); }else if (value1 != value2) { _Q.push(['diffValue', keyName, value1, value2, obj1]) } }; } for(var keyName in newO2){ _Q.push(['new', keyName, obj1[keyName], newO2[keyName], newO2]) } /* */ return _Q; }; // END compare()
以上是关于compare 2 json对象返回一个包含所有差异的数组的主要内容,如果未能解决你的问题,请参考以下文章
JavaComparable和Comparator接口的区别
LeetCode 165 Compare Version Numbers(比较版本号)(string)(*)