Set object 

  • The Set object is a collection of unique values, meaning that duplicate values are not allowed.

 

  • Set provides methods for adding, deleting, and checking the existence of values in the set.

 

  • Set can be used to remove duplicate values from arrays.

 

Useful - now to remove duplicates from an array

 

// How to remove duplicates from an array

let myArr = [1,4,3,4]

let mySet = new Set(myArray)


let uniqueArray = [...mySet]

// [1,4,3]

 

const uniqueNumbers = new Set()

uniqueNumbers.add(5)
uniqueNumbers.add(10)
uniqueNumbers.add(5)

console.log(uniqueNumbers)
// {5,10}

console.log(uniqueNumbers.size)
// 2

uniqueNumbers.has(10)
// true

uniqueNumbers.delete(10)
uniqueNumbers.size
// 1