Get unique values from an array


/ Published in: JavaScript
Save to your folder(s)

This code is used to get the unique values of an array.


Copy this code and paste it in your HTML
  1. const array = [1, 1, 1, 3, 3, 2, 2];
  2.  
  3. var unique = array.filter((element, index) => {
  4. return array.indexOf(element) === index;
  5. });
  6.  
  7. //unique.toString();
  8. for(var i=0;i<unique.length;i++){
  9. console.log(unique[i]+"\r\n<br>");
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.