WRX
0
Q:

javascript remove duplicate in arrays

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

let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
9
// 1. filter()
function removeDuplicates(array) {
  return array.filter((a, b) => array.indexOf(a) === b)
};
// 2. forEach()
function removeDuplicates(array) {
  let x = {};
  array.forEach(function(i) {
    if(!x[i]) {
      x[i] = true
    }
  })
  return Object.keys(x)
};
// 3. Set
function removeDuplicates(array) {
  array.splice(0, array.length, ...(new Set(array)))
};
// 4. map()
function removeDuplicates(array) {
  let a = []
  array.map(x => 
    if(!a.includes(x) {
      a.push(x)
    })
  return a
};
/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"
9
const numbers = [1 , 21, 21, 34 ,12 ,34 ,12];
const removeRepeatNumbers = array => [... new Set(array)]
removeRepeatNumbers(numbers) // [ 1, 21, 34, 12 ]
3
array1 = array1.filter(function(val) {
  return array2.indexOf(val) == -1;
});
// Or, with the availability of ES6:

array1 = array1.filter(val => !array2.includes(val));
0
unique = [...new Set(arr)];   // where arr contains duplicate elements
6
function toUniqueArray(a){
    var newArr = [];
    for (var i = 0; i < a.length; i++) {
        if (newArr.indexOf(a[i]) === -1) {
            newArr.push(a[i]);
        }
    }
  return newArr;
}
var colors = ["red","red","green","green","green"];
var colorsUnique=toUniqueArray(colors); // ["red","green"]
2
var myArr = [1, 2, 2, 2, 3];
var mySet = new Set(myArr);
myArr = [...mySet];
console.log(myArr);
// 1, 2, 3
1
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);
3
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'
0
arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)
2

New to Communities?

Join the community