Skip to content
Advertisements

JavaScript Set

Why Set ?

The JavaScript Set object is a feature supported by ES6. The set object can store any value, whether primitive or objects. No value is returned to the Set property. If you’ve added an element of the same value, it won’t add.

Syntax:

new Set([iterable])

iterable: If a renewable object is passed, all elements will be added to the new cluster.
If a value or parameter is not specified, the Set is empty.
The value is empty if null.


Set.prototype.size

The Set.prototype.size () property returns the number of items in an object. An access function set for you is undefined, you cannot change this feature.

Example:

        let mySet = new Set();
        let myObj = new Object();
        let myFucn = new Function();
        let myNumber = 100;
        let myWebsite = "truecodes.org";
        let myInstagram = "codeblogger"

        mySet.add(myObj);
        mySet.add(myFucn);
        mySet.add(myNumber)
        mySet.add(myNumber);
        mySet.add(myWebsite);
        mySet.add(myInstagram);

        console.log(mySet.size);

5

As shown in the example above. If you have more than the same value in “set”. It counts him as one.


Set.prototype.add()

It adds the new element with a specified value at the end of the Set object.

Syntax:

mySet.add(value);

value (required): The value to be added.

Example:

        let mySet = new Set();
        let myFunc = function () { };

        mySet.add("truecodes");
        mySet.add(57);
        mySet.add(myFunc);

        mySet.add("first").add("second").add("third");

        console.log(mySet);

0: “truecodes”
1: 57
2: function () { }
3: “first”
4: “second”
5: “third”


Set.prototype.clear()

The clear () method removes all values ​​in the “set”.

Syntax:

mySet.clear();

Example:

        mySet = new Set();

        mySet.add(1).add(2).add(3);
        console.log(mySet);

        mySet.clear();
        console.log(mySet);

Set(3) {1, 2, 3}
Set(0) {}


Set.prototype.delete()

The Set.prototype.delete () method deletes the specified item from the set object.

Syntax:

mySet.delete(value);

value(required): It is a value to be deleted from the set.

Example:

        mySet = new Set();

        mySet.add("first").add("second").add("third");
        console.log(mySet);

        mySet.delete("first");
        mySet.delete("third");
        console.log(mySet);

{“first”, “second”, “third”}
{“second”}


Set.prototype.entries()

Returns an iterator object that contains an array containing the entries of the set in the insertion scheme.

Syntax:

mySet.entries()

Example:

        let mySet = new Set();

        mySet.add("first");
        mySet.add("second");
        mySet.add("third");
        mySet.add("forth");
        mySet.add("fifth");
        
        let mySetEntries = mySet.entries();

        console.log(mySetEntries.next().value);
        console.log(mySetEntries.next().value);
        console.log(mySetEntries.next().value);
        console.log(mySetEntries.next().value);
        console.log(mySetEntries.next().value);

[“first”, “first”]
[“second”, “second”]
[“third”, “third”]
[“forth”, “forth”]
[“fifth”, “fifth”]


Set.prototype.forEach()

The Set.prototype.forEach () method executes a function once, provided by the order of insertion for each value in the Set object.

mySet.forEach(callback[,thisargument])

callback: It is a function which is to be executed for each element of the Set.
thisargument: Value to be used as this when executing callback.

callback is invoked with three arguments:

  • the element value
  • the element key
  • the Set object being traversed

Example:

        let mySet = new Set();

        mySet.add({
            name: "bob",
            surname: "queen"
        });
        mySet.add(1);
        mySet.add(2);
        mySet.add("first");
        mySet.add("second");

        function print(arr) {
            console.log(arr)
        };

        mySet.forEach(print)

{name: “bob”, surname: “queen”}
1
2
first
second


Set.prototype.has()

The Set.prototype.has () method controls the specified object. Returns true if the value specified in the “Set” object exists. Returns false if the value specified in the object does not exist.

Syntax:

mySet.has(value);

value (required):The value to be searched in the Set

Example:

        let mySet = new Set();
        mySet.add("codeblogger");
        mySet.add("truecodes");
        
        console.log(mySet.has("codeblogger"));
        console.log(mySet.has(12));
        console.log(mySet.has("truecodes"));
        console.log(30)

true
false
true
false


Set.prototype.values()

It returns all the values from the Set in the same insertion order.

Syntax:

mySet.values();

Example:

        let mySet = new Set();
        mySet.add("codeblogger");
        mySet.add("truecodes");
        mySet.add(1).add(2).add(3); 

        var iterator = mySet.values();
        
        console.log(iterator.next().value);
        console.log(iterator.next().value);
        console.log(iterator.next().value);
        console.log(iterator.next().value);
        console.log(iterator.next().value);

codeblogger
truecodes
1
2
3


Set.prototype.keys()

It also returns all values ​​in the Cluster during placement. It is also similar to the values ​​() value. It is even used in the same task.

Syntax:

mySet.keys();

Example:

        let mySet = new Set();
        mySet.add("codeblogger");
        mySet.add("truecodes");
        mySet.add(1).add(2).add(3); 

        var iterator1 = mySet.values();
        var iterator2 = mySet.keys();
        
        console.log(iterator1);
        console.log(iterator2);

{“codeblogger”, “truecodes”, 1, 2, 3}
{“codeblogger”, “truecodes”, 1, 2, 3}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: