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

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.