Javascript search for an object key in a set

JavascriptObject Oriented ProgrammingFront End Technology

The Set class in JavaScript provides a has method to search elements in a given set object. In case you want to search for a object in a set, you need to provide the reference to that object. Identical objects with different memory addresses are not considered equal. This method can be used as follows −

Example

let mySet = new Set();
let myObj = {name: "John"}
mySet.add(1);
mySet.add(3);
mySet.add("a");
mySet.add(myObj);
console.log(mySet)
console.log(mySet.has(myObj))
// Considered as a new object
console.log(mySet.has({name: "John"}))

Output

Set { 1, 2, 3, 'a' }
true
false
raja
Published on 18-Sep-2019 12:16:54
Advertisements