set into object code example

Example 1: array to set javascript

const myArray = [1,2,3,1,5,8,1,2,9,4];
const unique = [...new Set(myArray)]; // [1, 2, 3, 5, 8, 9, 4]

const myString = ["a","b","c","a","d","b"];
const uniqueString = [...new Set(myString)]; //["a", "b", "c", "d"]

Example 2: set in javascript

// set is used for storing unique values
const firstSet = new Set([1, 2, 3]);

firstSet.add('hi'); //adding value to set

firstSet.add(3); //this will not give any error and it will also not be added

firstSet.delete('hi');//removing value from set

console.log(firstSet.has('hi'));//checking 'hi' is in the set or not

// showing all values in the set
console.log(firstSet);
for (const entry of firstSet.values()) {
    console.log(entry);