A JavaScript array is a set of unique values.
Collections can hold values of any data type. They can be simple primitives, such as integers or strings, etc., or complex types such as arrays or literals. The values in the set can only occur once.
In this article, you will learn eight JavaScript Set methods that you should master today.
1. How to create a new group on JavaScript
You can create a new Set object using the new Puts() Building. 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);
Produce:
Set(5) { 21, 34, 35, 56, 23 }
If you do not specify anything in the parameter, a new empty set is created.
const setObj = new Set();
console.log(setObj);
Produce:
Set(0) {}
If you try to create a set with duplicate items, the duplicate items will be removed and a unique set will be returned.
const setObj = new Set([1, 2, 3, 1, 2, 3]);
console.log(setObj);
Produce:
Set(3) { 1, 2, 3 }
You can also create a group using mixed data types.
const setObj = new Set([1, 2, "Hello", [45, 67, 78]]);
console.log(setObj);
Produce:
Set(4) { 1, 2, 'Hello', [ 45, 67, 78 ] }
2. How to append new elements to a selected object
You can append a new element to the end of a Set object with Add() method. This method accepts the value of the element to be added 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);
Produce:
Set(3) { 'Welcome', 'to', 'MUO' }
You can pass the value directly 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);
Produce:
Set(3) { 'Welcome', 'to', 'MUO' }
The Add() The method also supports serialization.
const setObj = new Set();
// Chaining
setObj.add(1).add(2).add(3);
console.log(setObj);
Produce:
Set(3) { 1, 2, 3 }
If you try to append duplicate elements, only the first instance of the element 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);
Produce:
Set(6) { 1, 2, 3, 'M', 'U', 'O' }
3. How to remove all items from a group object
You can remove all items from a Set object with Clear() method. This method removes all items and restores undefined.
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);
Produce:
Size of the Set object: 5
Set(5) { 1, 2, 3, 4, 5 }
Size of the Set object after clearing elements: 0
Set(0) {}
4. How to delete a specific item from a specific object
You can delete a specific element from the Set object (if any) using the . extension delete() method. This method accepts the value to be deleted from the collection. If the value is found, the method returns real. 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);
Produce:
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 item exists in a collection
You can check if an element exists in a Set object with she has() method. This method accepts the value as parameter to test for presence in the Set object. If the element is found, the method returns real; Otherwise it will be False.
const setObj = new Set(["Welcome", "to", "MUO"]);
console.log(setObj.has("MUO"));
console.log(setObj.has("MakeUseOf"));
Produce:
true
false
6. What is the method of entries() in a JavaScript collection object?
According to the official MDN Web Docs:
“The entries() The method returns a new iterative object containing a group of [value, value] For each element in the Set object, in the order of insertion. For group objects, there is no such key as 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 the array is [value, value] is returned.”
const setObj = new Set(["Welcome", "to", "MUO"]);
for(let entry of setObj.entries()) {
console.log(entry);
}
Produce:
[ 'Welcome', 'Welcome' ]
[ 'to', 'to' ]
[ 'MUO', 'MUO' ]
const setObj = new Set(["Welcome", "to", "MUO"]);
for(let [key, value] of setObj.entries()) {
console.log(value);
}
Produce:
Welcome
to
MUO
The keys and values in the set are identical.
const setObj = new Set(["Welcome", "to", "MUO"]);
for(let [key, value] of setObj.entries()) {
console.log(key === value);
}
Produce:
true
true
true
7. What is the forEach() method in a JavaScript collection object?
The for every () The method calls a function for each element of the Set object. This method is back undefined.
const setObj = new Set([1, 2, 3, 4, 5]);
let sum = 0;
setObj.forEach(function(element) {
sum += element;
});
console.log(sum);
Produce:
15
8. What is the values() method in a JavaScript collection object?
The Value() How to return a file Repetition An object that contains all the values in an array, and does this 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);
}
Produce:
1
2
3
4
5
Noticeable: The Set object in JavaScript does not contain keys. But to make groups compatible with maps, the . file keys() The method is used as an alias for Value() method. The keys() The method behaves just like Value() method.
You now know how to create groups in JavaScript
So, there you are. After reading this article, you should have a lot to help you master creating collections on JavaScript.
Creating combos is crucial for any programmer looking to improve their game. Once you have mastered this, you can move on to mastering other skills – such as creating arrays.
read the following
About the author