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

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.