8 JavaScript Bundle Methods You Should Master Today

0

A JavaScript set is a collection of unique values.

Sets can contain values ​​of any data type. These can be simple primitives, such as integers or strings, etc., or complex types such as arrays or literal objects. Values ​​in the set can only appear once.

In this article, you’ll learn eight JavaScript Set methods that you should master today.

1. How to create a new set on JavaScript

You can create a new Set object using the New Adjust() builder. The Set object stores unique values ​​of any data type. This constructor accepts an iterable object as a parameter.

const setObj = new Set([21, 34, 35, 56, 23]);
console.log(setObj);

Go out:

Set(5) { 21, 34, 35, 56, 23 }

If you don’t specify anything in the parameter, a new, empty Set is created.

const setObj = new Set();
console.log(setObj);

Go out:

Set(0) {}

If you try to create a set with repeated items, the duplicate items will be removed and a single set will be returned.

const setObj = new Set([1, 2, 3, 1, 2, 3]);
console.log(setObj);

Go out:

Set(3) { 1, 2, 3 }

You can also create a set using mixed data types.

const setObj = new Set([1, 2, "Hello", [45, 67, 78]]);
console.log(setObj);

Go out:

Set(4) { 1, 2, 'Hello', [ 45, 67, 78 ] }

2. How to add new elements to a Set object

You can add a new element at the end of the Set object by using the add() method. This method accepts the value of the item to add to the Set object as a parameter and returns the new Set object with an added value.

const setObj = new Set();
setObj.add("Welcome");
setObj.add("to");
setObj.add("MUO");
console.log(setObj);

Go out:

Set(3) { 'Welcome', 'to', 'MUO' }

You can directly pass the value as a parameter, or you can pass the variable as one instead.

const setObj = new Set();
// Appending value to a Set object
setObj.add("Welcome");
const var1 = "to";
const var2 = "MUO";
// Appending variable to a Set object
setObj.add(var1);
setObj.add(var2);
console.log(setObj);

Go out:

Set(3) { 'Welcome', 'to', 'MUO' }

The add() The method also supports chaining.

const setObj = new Set();
// Chaining
setObj.add(1).add(2).add(3);
console.log(setObj);

Go out:

Set(3) { 1, 2, 3 }

If you try to add duplicate items, only the first instance of the item will be saved.

const setObj = new Set();
setObj.add(1);
setObj.add(2);
setObj.add(3);
setObj.add("M");
setObj.add("U");
setObj.add("O");
setObj.add(1);
setObj.add(2);
setObj.add(3);
console.log(setObj);

Go out:

Set(6) { 1, 2, 3, 'M', 'U', 'O' }

3. How to delete all elements of a defined object

You can remove all the elements of a Set object by using the clear () method. This method removes all items and returns indefinite.

const setObj = new Set([1, 2, 3, 4, 5]);
console.log("Size of the Set object: " + setObj.size);
console.log(setObj);
setObj.clear();
console.log("Size of the Set object after clearing elements: " + setObj.size);
console.log(setObj);

Go out:

Size of the Set object: 5
Set(5) { 1, 2, 3, 4, 5 }
Size of the Set object after clearing elements: 0
Set(0) {}

Related: JavaScript String Methods You Should Be Mastering Today

4. How to remove a specific element from a defined object

You can remove a specific element from a Set object (if it exists) using the wipe off() method. This method accepts the value to be removed from the set. If the value is found, the method returns true. Otherwise it will be false.

const setObj = new Set([1, 2, 3, 4, 5]);
console.log("Initial Set:");
console.log(setObj);
setObj.delete(3);
console.log("Set after deleting 3");
console.log(setObj);

Go out:

Initial Set:
Set(5) { 1, 2, 3, 4, 5 }
Set after deleting 3
Set(4) { 1, 2, 4, 5 }

5. How to check if an element exists in a set

You can check if an element exists in a Set object by using the To() method. This method accepts the value as a parameter to test for presence in the Set object. If the element is found, the method returns true; otherwise it will be false.

const setObj = new Set(["Welcome", "to", "MUO"]);
console.log(setObj.has("MUO"));
console.log(setObj.has("MakeUseOf"));

Go out:

true
false

6. What is the entry () method in the JavaScript Set object?

According to official MDN web documents:

“The inputs () The method returns a new Iterator object that contains a display of [value, value] for each element of the Set object, in the order of insertion. For Set objects, there is no key like in Map objects. However, to keep the API similar to the Map object, each entry has the same value for its key and value here, so an array [value, value] came back.”

const setObj = new Set(["Welcome", "to", "MUO"]);
for(let entry of setObj.entries()) {
console.log(entry);
}

Go out:

[ 'Welcome', 'Welcome' ]
[ 'to', 'to' ]
[ 'MUO', 'MUO' ]
const setObj = new Set(["Welcome", "to", "MUO"]);
for(let [key, value] of setObj.entries()) {
console.log(value);
}

Go out:

Welcome
to
MUO

The keys and values ​​of the set are the same.

const setObj = new Set(["Welcome", "to", "MUO"]);
for(let [key, value] of setObj.entries()) {
console.log(key === value);
}

Go out:

true
true
true

Related: How to Use Loops in JavaScript

7. What is the forEach () method in the JavaScript Set object?

The for each() The method calls a function for each element of the Set object. This method returns indefinite.

const setObj = new Set([1, 2, 3, 4, 5]);
let sum = 0;
setObj.forEach(function(element) {
sum += element;
});
console.log(sum);

Go out:

15

8. What is the values ​​() method in the JavaScript Set object?

The values() the method returns a Iterator object that contains all the values ​​in a set, and it does so in the order of insertion.

const setObj = new Set([1, 2, 3, 4, 5]);
const iteratorObj = setObj.values();
for (let value of iteratorObj) {
console.log(value);
}

Go out:

1
2
3
4
5

Related: Introduction to Iterators and Generators in JavaScript

To note: A Set object in JavaScript has no keys. But to make Sets compatible with Maps, the keys () method is used as an alias for the values() method. The keys () method behaves exactly the same as the values() method.

You now know how to create sets in JavaScript

So this is it. After reading this article, you should have plenty of stuff to help you master creating sets in JavaScript.

Creating sets is crucial for any programmer looking to improve their game. Once you’ve mastered this, you can move on to honing other skills, such as creating tables.


javascript-array-methods
15 JavaScript array methods you should master today

Want to understand JavaScript tables but can’t seem to master them? See our example JavaScript tables for tips.

Read more


About the Author


Source link

Share.

About Author

Leave A Reply