Every line of 'compare two json objects javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.
5 function compareJson(objX, objY, result) { 6 7 result = result || []; 8 9 // 基础类型 10 11 12 // 对象类型 13 for (var key in objY) { 14 if (objY[key] !== objX[key]) { 15 if (!(key in objX)) { 16 result.push({ 17 action: 'delete', 18 key: key 19 }); 20 continue; 21 } 22 compareJson(objX[key], objY[key], result.children = []); 23 } 24 } 25 for (var key in objX) { 26 if (!(key in objY)) { 27 result.push({ 28 action: "add", 29 key: key 30 }); 31 } 32 } 33 34 35 return result; 36 }
69 function compareJsonRef(beforeJsonRef, afterJsonRef) { 70 beforeJsonRef = beforeJsonRef.replace(/.*\.json:\d+:\d+/, '') 71 afterJsonRef = afterJsonRef.replace(/.*\.json:\d+:\d+/, '') 72 73 return (beforeJsonRef == afterJsonRef); 74 }
543 function compareObjectArrays(a, b) { 544 expect(a.length).toEqual(b.length); 545 546 for (let i = 0, l = a.length; i < l; i++) { 547 for (const prop in a[i]) { 548 if ( hasOwnProp.call(a[i], prop) ) { 549 expect(b[i][prop]).toBeDefined(); 550 expect(a[i][prop]).toEqual(b[i][prop]); 551 } 552 } 553 } 554 }
160 function compareObjects(a, b, options) { 161 let diffMessage; 162 let hasThrown = false; 163 164 try { 165 diffMessage = (0, _diffLines.default)( 166 (0, _prettyFormat.default)(a, FORMAT_OPTIONS_0), 167 (0, _prettyFormat.default)(b, FORMAT_OPTIONS_0), 168 options, 169 { 170 a: (0, _prettyFormat.default)(a, FORMAT_OPTIONS), 171 b: (0, _prettyFormat.default)(b, FORMAT_OPTIONS) 172 } 173 ); 174 } catch (e) { 175 hasThrown = true; 176 } // If the comparison yields no results, compare again but this time 177 // without calling `toJSON`. It's also possible that toJSON might throw. 178 179 if (!diffMessage || diffMessage === _constants.NO_DIFF_MESSAGE) { 180 diffMessage = (0, _diffLines.default)( 181 (0, _prettyFormat.default)(a, FALLBACK_FORMAT_OPTIONS_0), 182 (0, _prettyFormat.default)(b, FALLBACK_FORMAT_OPTIONS_0), 183 options, 184 { 185 a: (0, _prettyFormat.default)(a, FALLBACK_FORMAT_OPTIONS), 186 b: (0, _prettyFormat.default)(b, FALLBACK_FORMAT_OPTIONS) 187 } 188 ); 189 190 if (diffMessage !== _constants.NO_DIFF_MESSAGE && !hasThrown) { 191 diffMessage = _constants.SIMILAR_MESSAGE + '\n\n' + diffMessage; 192 } 193 } 194 195 return diffMessage; 196 } // eslint-disable-next-line no-redeclare