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

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.