Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active March 23, 2018 15:10
Show Gist options
  • Select an option

  • Save tommcfarlin/c61553ce8e662776a4d1fb7e538de55f to your computer and use it in GitHub Desktop.

Select an option

Save tommcfarlin/c61553ce8e662776a4d1fb7e538de55f to your computer and use it in GitHub Desktop.
[JavaScript] Removing Duplicate Items with JavaScript
/**
* 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