How to know whether a value is searched in Javascript sets?


In javascript, we may use some sets to hold a few objects. Sometimes we want to check whether an element is present inside a set or not. In this article, we will see different techniques to check whether an element is inside a given set or not.p>

A trivial method using for loop

At first, we can search for an element by manually picking an element and checking whether it is the same as the key element or not. If so then simply return true, otherwise after completing all elements return false.

Examples

<!DOCTYPE html> <html> <head> <title>HTML Console</title> </head> <body> <h3> Output Console </h3> <p> Output: </p> <div id="output"> </div> <div id="opError" style="color : #ff0000"> </div> <script> var content = '' var error = '' var opDiv = document.querySelector('#output') var opErrDiv = document.querySelector('#opError') // actual javascript code try { // Creating a promise const mySet = new Set(['one', 'two', 3, 4, 5, 'SIX', '7']); function searchInSet(set, key) { for (const element of mySet) { if (element === key) { return true; } } return false; } content += "Is 4 present in the set: " + JSON.stringify(searchInSet(mySet, 4)) + "<br>"; content += "Is '7' present in the set: " + JSON.stringify(searchInSet(mySet, '7')) + "<br>"; content += "Is 40 present in the set: " + JSON.stringify(searchInSet(mySet, 40)) + "<br>"; } catch (err) { error += err } finally { // display on output console opDiv.innerHTML = content opErrDiv.innerHTML = error } </script> </body> </html>

Using has() function

Javascript Set classes have one property called has(), this function checks whether the given element is present inside the current set or not. If this is present, then return true, otherwise, return false.

Syntax

set_object.has(key)

Examples

<!DOCTYPE html> <html> <head> <title>HTML Console</title> </head> <body> <h3> Output Console </h3> <p> Output: </p> <div id="output"> </div> <div id="opError" style="color : #ff0000"> </div> <script> var content = '' var error = '' var opDiv = document.querySelector('#output') var opErrDiv = document.querySelector('#opError') // actual javascript code try { // Creating a promise const s = new Set(); s.add('java'); content += "Is java present? " + JSON.stringify(s.has('java')) + "<br>"; // returns true as java is present content += "Is javascript present?" + JSON.stringify(s.has('javascript')) + "<br>"; // returns false because this is not present const s1 = new Set(); const ob1 = { 'c++': 1 }; s1.add(ob1); content += "ob1 is in the set? " + JSON.stringify(s1.has(ob1)) + "<br>"; // returns true content += "C++ is in the set? " + JSON.stringify(s1.has({ 'c++': 1 })) + "<br>"; // returns false because they are different object references } catch (err) { error += err } finally { // display on output console opDiv.innerHTML = content opErrDiv.innerHTML = error } </script> </body> </html>

Conclusion

In this article, we have seen how to check whether a given element is present inside a set object or not. We can do this manually by checking each element one by one. This process can also be used for arrays and other data structures, but in sets, there will be no duplicate elements. Another simpler approach is using has() function, which comes with the Set object in javascript. It directly returns true when an object is present in the set, otherwise returns false.

Updated on: 23-Aug-2022

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements