Skip to main content

Set collection in Javascript

 A Set is a data structure called a collection, and a Map is a data structure called a dictionary.Both sets and dictionaries can store unique values, which are convenient for deduplication. Sets store elements in the form of [value, value], and dictionaries store elements in the form of [key, value].A Set can hold any value of any data type. Let's first briefly understand the properties and methods of the collection, and then study the specific use of this collection.


Create a Set instance

An empty collection can be created using the new keyword and the Set constructor. If you want to initialize the instance at the same time, you can pass an iterable object to the Set constructor.

        let set=new Set();
        // console.log(set);
        let set1=new Set([1,2,3]);

Common method: size attribute

        let set1=new Set([1,2,3]);
        console.log(set1.size)//3

add() method

        const s = new Set();
        s.add(1).add(2).add(3);
        console.log(s);

delete() method

        const s = new Set();
        s.add(1).add(2).add(3);
        s.delete(1)
        console.log(s);//{2,3}

clear() method

        const s = new Set();
        s.add(1).add(2).add(3);
        s.delete(1)
        s.clear();
        console.log(s);

has() method: Query whether an element exists in the Set instance (returns a boolean value):

        const s = new Set();
        s.add(1).add(2).add(3);
        s.delete(1)
        s.clear();
        console.log(s.has(2));//false

Convert data of type set to data of array type

        const s=new Set([1,2,3])
        const arr=[...s];
        console.log(arr);
The entries() method returns a new Iterator object containing the [value, value] array for each element in the Set
        var s = new Set(["aaa","bbb","ccc"]);
        var iterator = s.entries()
        console.log(iterator.next().value); //["aaa", "aaa"]
        console.log(iterator.next().value); //["bbb", "bbb"]
        console.log(iterator.next().value); //["ccc", "ccc"]

keys() method: returns the key name, values() method: returns the key value

        var s = new Set(["aaa","bbb","ccc"]);
        var iterator = s.keys()
        var value=s.values();
        console.log(iterator);//SetIterator {"aaa", "bbb", "ccc"}
        console.log(value);//SetIterator {"aaa", "bbb", "ccc"}

Intersect can be easily implemented using Set

        const arr1=[1,2,3,4,5,6];
        const arr2=[4,5,6,7,8];
        const s=new Set(arr2);
        const arr3=arr1.filter(value=>{
            return s.has(value);
        })
        console.log(arr3);//[4,5,6]

Difference can be easily implemented using Set

        const arr1=[1,2,3,4,5,6];
        const arr2=[4,5,6,7,8];
        const s=new Set(arr2);
        const arr3=arr1.filter(value=>{
            return !s.has(value);
        })
        console.log(arr3);//[1,2,3]

Union can be easily implemented using Set

        const arr1=[1,2,3,4,5,6];
        const arr2=[4,5,6,7,8];
        const arr3=new Set([...arr1,...arr2]);
        console.log(arr3);//{1,2,3,4,5,6,7,8}

Comments

Popular posts from this blog

Destructuring assignment in ES6

 In the past, when we wanted to assign a value to a variable, such as an array type and an object type, to assign a value to a variable, we could only specify the value directly. It is very troublesome to write in this way, but under the ES6 syntax specification, it is allowed to directly extract the required values from arrays and objects according to a certain pattern, and directly assign variables to variables. This method is called Destructuring, which is simple to understand. That is, the left and right sides of the equal sign are equal.

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.