delete duplicate values in array javascript code example

Example 1: javascript remove duplicates from array

unique = [...new Set(arr)];   // where arr contains duplicate elements

Example 2: how to remove duplicates in array in javascript

const numbers = [1 , 21, 21, 34 ,12 ,34 ,12];
const removeRepeatNumbers = array => [... new Set(array)]
removeRepeatNumbers(numbers) // [ 1, 21, 34, 12 ]

Example 3: javascript remove duplicates from array

var myArr = [1, 2, 2, 2, 3];
var mySet = new Set(myArr);
myArr = [...mySet];
console.log(myArr);
// 1, 2, 3

Example 4: js delete duplicates from array

const names = ['John', 'Paul', 'George', 'Ringo', 'John'];

function removeDups(names) {
  let unique = {};
  names.forEach(function(i) {
    if(!unique[i]) {
      unique[i] = true;
    }
  });
  return Object.keys(unique);
}

removeDups(names); // // 'John', 'Paul', 'George', 'Ringo'

Example 5: remove duplicates array javascript

var data = [1, 2, 'Peter', 3, 3,'Peter', 2, 2, 1, 2, 3, 4, 4, 5, 4];
var unique = data.filter((v, i, a) => a.indexOf(v) === i);