Mounir Federere Mounir Federere - 1 year ago 55
Javascript Question

jquery to pure javascript remove duplicate elments

i have this script to remove duplicate elements by class using jquery, is there a way to do it with pure javascript? this is my code

<script >$(document).ready(function() {
var seen2 = {};
$('.listofarticlescolor').each(function() {
var txt2 = $(this).text();
if (seen2[txt2])
$(this).closest('.category').remove();
else
seen2[txt2] = true;
});

});
</script>

Answer Source

Try this code, to make it work first you have to convert the list of nodes in an array, then do something similar to what you've done.

var classWillRemove = [].slice.call(document.getElementsByClassName('listofarticlescolor'));
var seen2 = {};
for(i=0;i< classWillRemove.length;i++){
     var text = classWillRemove[i].innerHTML;
     if (seen2[text]){
         classWillRemove[i].parentNode.removeChild(classWillRemove[i]);
    } else {
         seen2[text] = true;
    }
}

Demo :http://jsfiddle.net/64fu2qqe/