how to remove same element from array in javascript code example

Example 1: javascript remove duplicate in arrays

// 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"

Example 2: remove duplicates from array javascript

[...new Set(array)]

Example 3: 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 4: javascript to remove duplicates from an array

uniqueArray = a.filter(function(item, pos) {
    return a.indexOf(item) == pos;
})

Example 5: remove duplicates from array javascript

arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)

Tags:

Css Example