标签:需求 UNC mda call atd prot cas ndt efi
工作了这么久,第一次记录心得,有些汗颜,因为某些原因,觉得还是记录下比较好,话不多说,现在开始。
这个需求的背景是我有一个表单,表单对象有array,obj,str等,我现在对这个表单数据某些表单项做了修改,保存的时候需要对比修改前后的表单,列出哪些修改项,来触发某些操作。
下面是方法:
1 // 对比JSON数据 2 export function equalsWithA (object1, object2) { 3 var changeField = [] 4 for (var propName in object1) { 5 if (!object1[propName] && !object2[propName]) { 6 continue 7 } 8 if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName) || typeof object1[propName] !== typeof object2[propName]) { 9 changeField.push(propName) 10 continue 11 } 12 if (object1[propName] instanceof Array) { 13 if (!arrayEquals(object1[propName], object2[propName])) { 14 changeField.push(propName) 15 continue 16 } 17 } else if (object1[propName] instanceof Object && object2[propName] instanceof Object) { 18 if (!jsonEquals(object1[propName], object2[propName])) { 19 changeField.push(propName) 20 continue 21 } 22 } else if (typeof object1[propName] === ‘string‘ && typeof object2[propName] === ‘string‘) { 23 try { 24 var d1 = ‘‘ 25 var d2 = ‘‘ 26 if (propName.indexOf(‘EndTime‘) > -1 || propName.indexOf(‘StartTime‘) > -1) { 27 d1 = formatDate(object1[propName]) 28 d2 = formatDate(object2[propName]) 29 } else { 30 d1 = object1[propName] 31 d2 = object2[propName] 32 } 33 34 if (d1 && d2) { 35 if (d1 != d2) { 36 // 日期类型 37 changeField.push(propName) 38 } 39 continue 40 } else if (object1[propName] != object2[propName]) { 41 changeField.push(propName) 42 continue 43 } 44 } catch (e) { } 45 } else if (object1[propName] != object2[propName]) { 46 changeField.push(propName) 47 continue 48 } 49 } 50 return changeField 51 }; 52 function arrayEquals (object1, object2) { 53 if (object1.length != object2.length) { 54 return false 55 } 56 object1.sort() 57 object2.sort() 58 for (var i = 0; i < object1.length; i++) { 59 if (typeof object1[i] !== typeof object2[i]) { 60 return false 61 } 62 if (object1[i] instanceof Array && object2[i] instanceof Array) { 63 if (!arrayEquals(object1[i], object2[i])) { 64 return false 65 } 66 } else if (isJson(object1[i]) && isJson(object2[i])) { 67 if (!jsonEquals(object1[i], object2[i])) { 68 return false 69 } 70 } else if (object1[i] != object2[i]) { 71 return false 72 } 73 } 74 return true 75 }; 76 function jsonEquals (object1, object2) { 77 for (var propName in object1) { 78 if (!object1[propName] && !object2[propName]) { 79 continue 80 } 81 if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName) || typeof object1[propName] !== typeof object2[propName]) { 82 return false 83 } 84 if (object1[propName] instanceof Array) { 85 if (!arrayEquals(object1[propName], object2[propName])) { return false } 86 } else if (isJson(object1[propName])) { 87 if (!jsonEquals(object1[propName], object2[propName])) { return false } 88 } else if (typeof object1[propName] === ‘string‘ && typeof object2[propName] === ‘string‘) { 89 try { 90 var d1 = ‘‘ 91 var d2 = ‘‘ 92 if (propName.indexOf(‘EndTime‘) > -1 || propName.indexOf(‘StartTime‘) > -1) { 93 d1 = formatDate(object1[propName]) 94 d2 = formatDate(object2[propName]) 95 } else { 96 d1 = object1[propName] 97 d2 = object2[propName] 98 } 99 if (d1 && d2 && d1 != d2) { 100 // 日期类型 101 return false 102 } else if (object1[propName] != object2[propName]) { 103 return false 104 } 105 } catch (e) { } 106 } else if (object1[propName] != object2[propName]) { 107 return false 108 } 109 } 110 return true 111 }; 112 function isJson (obj) { 113 var isjson = typeof (obj) === ‘object‘ && Object.prototype.toString.call(obj).toLowerCase() == ‘[object object]‘ && !obj.length 114 return isjson 115 }
调用方法
标签:需求 UNC mda call atd prot cas ndt efi
原文地址:https://www.cnblogs.com/xiami2104/p/11783104.html