0

I have so many categories on the page and using each function in jquery, I am listing all of them. I want to display only unique categories from the list and not the repeated ones. What should I use in jquery?

 jQuery(document).ready(function() {
    jQuery( ".rpc-post-category a" ).each( function( index, element ){
        console.log( jQuery( this ).text() );
    });
});

Note: I want a list of unique values from all text values.

Alex Mayo
  • 82
  • 1
  • 10

1 Answers1

0

You can use filter to get unique value ref Get all unique values in a JavaScript array (remove duplicates) .Hope this is what you are looking for,thanks

$(document).ready(function() {
   var result=[]
   $( ".class_name" ).each( function( index, element ){
    result.push( $( this ).text());
});
 result = result.filter((v, i, a) => a.indexOf(v) === i); 
 console.log(result)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="class_name">a</a>
<a href="#" class="class_name">b</a>
<a href="#" class="class_name">a</a>
<a href="#" class="class_name">c</a>
<a href="#" class="class_name">d</a>
Deepak A
  • 1,624
  • 1
  • 7
  • 16