remove duplicates from array in javascript code example

Example 1: js delete duplicates from array

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

let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'

Example 2: javascript dedupe array

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

let x = (names) => names.filter((v,i) => names.indexOf(v) === i)
x(names); // 'John', 'Paul', 'George', 'Ringo'

Example 3: javascript remove duplicates from array

// for TypeScript and JavaScript
const initialArray = ['a', 'a', 'b',]
finalArray = Array.from(new Set(initialArray)); // a, b

Example 4: javascript to remove duplicates from an array

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

Example 5: javascript remove duplicates from array

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

Example 6: javascript filter array remove duplicates

// Using the Set constructor and the spread syntax:

arrayWithOnlyUniques = [...new Set(arrayWithDuplicates)]

Tags:

Css Example