Created
December 17, 2018 17:09
-
-
Save cmatschiner/b73cacce7eb95b18d3a9c37cda1c1bab to your computer and use it in GitHub Desktop.
Eloquent Java Script – Deep comparison created by cmatschiner - https://repl.it/@cmatschiner/Eloquent-Java-Script-Deep-comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>repl.it</title> | |
| <link href="style.css" rel="stylesheet" type="text/css" /> | |
| </head> | |
| <body> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script> | |
| <script src="script.js"></script> | |
| </body> | |
| </html> | |
| xxss |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Function | |
| function deepEqual(inputA, inputB) { | |
| // if both identical e.g. object instance, array or primitive value return true | |
| if (inputA === inputB) return true; | |
| // if both are objects and values not null start deep comparison (DC) | |
| if ( | |
| typeof inputA === "object" && | |
| inputA !== null && | |
| (typeof inputB === "object" && inputA !== null) | |
| ) { | |
| // DC: Deep comparison | |
| // initiate query of all object properties and/or array elements | |
| for (var keyA in inputA) { | |
| keyB = Object.keys(inputB)[0]; | |
| valueA = Object.values(inputA)[0]; | |
| valueB = Object.values(inputB)[0]; | |
| // DC 1: if object value is an object then initiate anaylses of nested object(s) | |
| if (typeof valueA === "object" || typeof valueB === "object") { | |
| // DC 1.1: if cuurent keys differ return false | |
| if (keyA !== keyB) return false; | |
| // DC 1.1.2: if current values differ return false | |
| if ( | |
| (typeof valueA !== "object" || typeof valueB !== "object") && | |
| valueA !== valueB | |
| ) | |
| return false; | |
| // DC 1.2: delete current key and shift nested object one level down | |
| delete inputA[keyA]; | |
| delete inputB[keyB]; | |
| let keyValueA = Object.keys(valueA)[0]; | |
| let valueValueA = Object.values(valueA)[0]; | |
| let keyValueB = Object.keys(valueB)[0]; | |
| let valueValueB = Object.values(valueB)[0]; | |
| inputA[keyValueA] = valueValueA; | |
| inputB[keyValueB] = valueValueB; | |
| // DC 1.3 return to function for further deep scan of remeaining object properties or array elements | |
| return deepEqual(inputA, inputB); | |
| } | |
| // DC 2: coompare array or object and return false if not identical | |
| else if (keyA !== keyB) return "false"; | |
| if (valueA !== valueB) return false; | |
| // delete first object property or elemnt array | |
| delete inputA[keyA]; | |
| delete inputB[keyB]; | |
| // return to function for further deep scan of remeaining object properties or array elements | |
| return deepEqual(inputA, inputB); | |
| } | |
| // return false if only one object property or array element is undefined | |
| if (Object.values(inputA)[0] === undefined && Object.values(inputB)[0] !== undefined) return false; | |
| return true; | |
| } | |
| return false; | |
| } | |
| // Test variables | |
| let obj0 = { here: { is: "a" }, object: 2 }; | |
| let obj1 = { here: { is: "a" }, object: 2 }; | |
| let obj2 = { here: { is: "a" }, object: 3 }; | |
| let obj3 = { here: { is: "b" }, object: 2 }; | |
| let obj = {}; | |
| let array1 = [1, 2, 3, 4]; | |
| let array2 = [1, 2, 3, 5]; | |
| // Output | |
| console.log(deepEqual(obj0, obj1)); | |
| console.log(deepEqual(obj1, obj2)); | |
| console.log(deepEqual(obj2, obj3)); | |
| console.log(deepEqual(obj1, obj3)); | |
| console.log(deepEqual(obj, obj)); | |
| console.log(deepEqual(array1, array2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Empty file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment