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 the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.