Last active
March 23, 2018 15:10
-
-
Save tommcfarlin/c61553ce8e662776a4d1fb7e538de55f to your computer and use it in GitHub Desktop.
[JavaScript] Removing Duplicate Items with JavaScript
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
| /** | |
| * Removes duplicates from the specified array of items; however, this function | |
| * assumes that $items is an unordered list of items with child elements. | |
| * | |
| * @param $items An unordered list of products with duplicaten children. | |
| * @return $items An unordered list of products no duplicate children. | |
| * | |
| * @private | |
| */ | |
| var _removeDuplicates = function($items) { | |
| var unique = {}, | |
| dataItemId = -1; | |
| $items | |
| .children() | |
| .each(function() { | |
| dataItemId = $(this).attr('data-id'); | |
| if (unique[dataItemId]) { | |
| $(this).remove(); | |
| } else { | |
| unique[dataItemId] = true; | |
| } | |
| }); | |
| return $item; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment