WebMasterCampus
WEB DEVELOPER Resources

Remove Duplicates Array Values using Javascript

Learn how to Remove Duplicates Array Values using Javascript


Remove Duplicates Array Values using ES5

Using ECMAScript 5 we can use the filter method of an Array to get an array with absolutely unique values.


  function getUnique(value, index, self) {
      return self.indexOf(value) === index;
  }

  //let myList = ['sam','ram', 'sam', 'dam', 'game', 'game']
  var myList = [ 2, 4, 6, 6, 8, 8, 10 ];
  var uniqueList2 = myList.filter( getUnique );

  console.log( uniqueList);

Remove Duplicates Array Values using ES6

ES6 which was introduce in 2015 has a new data structure called “Set”. Set data structure store unique values. To get an array with unique values we can use following code.


  const myList2 = [ 2, 4, 6, 6, 8  ];

  const uniqueList = [...new Set(myList2)]

  console.log( uniqueList);
Created with love and passion.