Set in JavaScript. Code examples open

Set in JavaScript. Code examples

Sets represent a data structure that can only store unique values. In JavaScript, the set functionality defines a Set object.

    const mySet = new Set();

Passing an array of values to the constructor:

    const arr = [1, 1, 2, 3, 4, 5, 2, 4];
    const numbers = new Set(arr);
    console.log(numbers); // Set(5) {1, 2, 3, 4, 5}

A set can store only unique values, then when creating it, the repeated values that are in the array ,
are removed.

You can use the size method to check the number of elements.

    const arr = [1, 1, 2, 3, 4, 5, 2, 4];
    const numbers = new Set(arr);
    console.log(numbers.size); //5

The add method is used to add data:

    const mySet = new Set();
    mySet.add(1);
    mySet.add(3);
    mySet.add(5);

Since the add method returns a reference to the same set, we can call methods in a chain:

    mySet.add(1).add(3).add(5);

The delete method is used to remove elements:

    let mySet = new Set();
    mySet.add(1).add(3).add(5);
    mySet.delete(3);
    console.log(mySet); //Set(2) {1, 5}
This method returns a boolean value: true – if the element is removed and false – if the removal did not occur (for example, when the element to be removed is not in the set).

The clear method removes all elements from the set:

    let mySet = new Set();
    mySet.add(1).add(3).add(5);
    mySet.clear();
    console.log(mySet);     

If you need to check if an element is in a set, then the has() method is used. If the element exists, then the method returns true, otherwise it returns false:

    let mySet = new Set();
    mySet.add(1).add(3).add(5);
    console.log(mySet.has(3));    // true
    console.log(mySet.has(32));   // false

Iterating set

The forEach() method is used to iterate over the elements of a set:

    const arr = [1, 2, 3, 5];
    const mySet = new Set(arr);

    numbers.forEach(function (value1, value2, set) {
        console.log(value1);
    })
//Console: 1
2
3
5

For a set directly, the first and second parameters represent the current element being iterated over; the third parameter is the iterable set.

Iterating with a for of loop

    const mySet = new Set([1, 2, 3, 5]);

    for (n of mySet ) {
        console.log(n);
    }

Getting an iterator

The Set object also has a number of methods that return an iterator, more specifically a SetIterator object. These are the methods : values() , keys(), entries():

    const numbers = new Set([1, 2, 3, 5]);

    console.log(numbers.values());  // SetIterator {1, 2, 3, 5}
    console.log(numbers.keys());    // SetIterator {1, 2, 3, 5}
    console.log(numbers.entries()); // SetIterator {1 => 1, 2 => 2, 3 => 3, 5 => 5}

The returned iterator we can use to get set objects:

    const people = new Set(["Tom", "Bob", "Sam"]);

    const iterator = people.values();

    console.log(iterator.next());   // {value: "Tom", done: false}
    console.log(iterator.next());   // {value: "Bob", done: false}
    console.log(iterator.next());   // {value: "Sam", done: false}
    console.log(iterator.next());   // {value: undefined, done: true}

Remove duplicate elements

    const peopleArray = ["Tom", "Bob", "Sam", "Alice", "Sam", "Kate", "Tom"];
    const peopleSet = new Set(peopleArray);
    const newPeopleArray = Array.from(peopleSet);

    console.log(newPeopleArray);    // ["Tom", "Bob", "Sam", "Alice", "Kate"]

Here, to create a new array with non-repeating elements, the Array.from() function is used, which, as
argument receives a Set object.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*