- Notifications
You must be signed in to change notification settings - Fork 128
add snippet to compare two javascript arrays for equality #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
24cda3d add snippet to compare two javascript arrays for equality
KCSquid a06fbfb update compare-arrays to validate arrays and support nested arrays
KCSquid 472bbfb add support for object comparison in arrays (new function compareObjeβ¦
KCSquid 6d4d53a make significantly shorter, use ternary operator for comparisons whenβ¦
KCSquid 0b414af merge with current state of main
KCSquid b1da7e7 merge with current state of main
KCSquid 4b60765 Merge branch 'compare-arrays' of https://github.com/KCSquid/quicksnipβ¦
KCSquid c73bd17 delete random quicksnip folder
KCSquid 6d19464 made description for compare-arrays more descriptive
KCSquid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make significantly shorter, use ternary operator for comparisons whenβ¦
β¦ possible, switch to strict equality, use every instead of loop for one liner, add object tag
- Loading branch information
commit 6d4d53a2f7f644bc00a78130a9888fcf8e44dea1
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -2,64 +2,31 @@ | |
| title: Compare Arrays | ||
| description: Compares two arrays to check if they are equal. | ||
| ||
| author: KCSquid | ||
| tags: array,compare,equal | ||
| tags: array,object,compare,equal | ||
| --- | ||
| | ||
| ```js | ||
| const compareArrays = (array1, array2) => { | ||
| if ( | ||
| !Array.isArray(array1) || !Array.isArray(array2) || | ||
| array1.length !== array2.length | ||
| ) return false; | ||
| | ||
| for (let i = 0; i < array1.length; i++) { | ||
| if (Array.isArray(array1[i]) && Array.isArray(array2[i])) { | ||
| if (!compareArrays(array1[i], array2[i])) return false; | ||
| } else if (typeof array1[i] === "object" && typeof array2[i] === "object") { | ||
| if (!compareObjects(array1[i], array2[i])) return false; | ||
| } else if (array1[i] !== array2[i]) { | ||
| return false; | ||
| } | ||
| } | ||
| | ||
| return true; | ||
| const compareArrays = (a, b) => { | ||
| if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false; | ||
| return a.every((v, i) => | ||
| Array.isArray(v) && Array.isArray(b[i]) ? compareArrays(v, b[i]) : | ||
| typeof v === "object" && typeof b[i] === "object" ? compareObjects(v, b[i]) : | ||
| v === b[i] | ||
| ); | ||
| }; | ||
| | ||
| const compareObjects = (obj1, obj2) => { | ||
| if (typeof obj1 !== "object" || typeof obj2 !== "object") return false; | ||
| | ||
| const keys1 = Object.keys(obj1); | ||
| const keys2 = Object.keys(obj2); | ||
| if (keys1.length !== keys2.length) return false; | ||
| | ||
| for (let key of keys1) { | ||
| const val1 = obj1[key]; | ||
| const val2 = obj2[key]; | ||
| | ||
| if (Array.isArray(val1) && Array.isArray(val2)) { | ||
| if (!compareArrays(val1, val2)) return false; | ||
| } else if (typeof val1 === "object" && typeof val2 === "object") { | ||
| if (!compareObjects(val1, val2)) return false; | ||
| } else if (val1 !== val2) { | ||
| return false; | ||
| } | ||
| } | ||
| | ||
| return true; | ||
| const compareObjects = (a, b) => { | ||
| if (typeof a !== "object" || typeof b !== "object" || Object.keys(a).length !== Object.keys(b).length) return false; | ||
| return Object.keys(a).every(k => | ||
| Array.isArray(a[k]) && Array.isArray(b[k]) ? compareArrays(a[k], b[k]) : | ||
| typeof a[k] === "object" && typeof b[k] === "object" ? compareObjects(a[k], b[k]) : | ||
| a[k] === b[k] | ||
| ); | ||
| }; | ||
| | ||
| // Usage: | ||
| const number = 123; | ||
| const array1 = [1, 2, 3, 4, 5]; | ||
| const array2 = [1, 2, 3, 4, 5]; | ||
| const array3 = [[1, 2], [3, 4]]; | ||
| const array4 = [[1, 2], [3, 4]]; | ||
| const array5 = [{ a: 1, b: [{ c: 2 }] }, 3]; | ||
| const array6 = [{ a: 1, b: [{ c: 2 }] }, 3]; | ||
| | ||
| compareArrays(array1, array2); // Returns: true | ||
| compareArrays(array3, array4); // Returns: true | ||
| compareArrays(array5, array6); // Returns: true | ||
| compareArrays(array1, number); // Returns: false | ||
| compareArrays(array3, array6); // Returns: false | ||
| compareArrays([1, 2, 3], [1, 2, 3]); // Returns: true | ||
| compareArrays([1, 2, 3], [3, 2, 1]); // Returns: false | ||
| compareArrays([{a:1}], [{a:1}]); // Returns: true | ||
| compareArrays([{a:1}], null); // Returns: false | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.