Javascript search for an object key in a set

In JavaScript, to search for an object key in a Set, you need to understand how Sets store values and how objects behave in JavaScript. JavaScript Sets are useful objects that store unique values, making them excellent for handling distinct data. In this article, we'll explore different ways to search for an object key in a Set.

JavaScript Set

A Set in JavaScript is a collection of unique values. Values like numbers and strings can be compared directly, but objects are stored by reference. This means that even if two objects have the same properties, they are still seen as different.

const obj1 = { name: "John" };
const obj2 = { name: "John" };
const mySet = new Set([obj1, obj2]);

console.log(mySet.size);  // 2 - both objects are stored
console.log(mySet.has(obj1));  // true
console.log(mySet.has({ name: "John" }));  // false - different reference
2
true
false

Searching for an Object Key in a Set

If you want to find an object by its key or property value, you can't use the Set.has() method directly. You can use the following methods mentioned below:

Using a Loop

Using a loop is an easier way to search for an object key in a set. Using a for?of loop, you can manually check for the desired key or value.

Example

The following is a simple example for searching an object key in a set using for... of loop.

// Create a list of objects with different values
const myObjects = [
    { name: "x", value: 10 },
    { name: "y", value: 20 },
    { name: "z", value: 30 }
];

// Make a Set from the list of objects
const mySet = new Set(myObjects);

// Function to find an object by its key and value
function findObjectByKey(set, key, value) {
    // Go through each object in the Set
    for (const obj of set) {
        // Check if the object's key matches the value we're looking for
        if (obj[key] === value) {
            return obj; // Return the object if we find a match
        }
    }
    return null; // Return null if no match is found
}

// Use the function to find an object with name "y"
console.log(findObjectByKey(mySet, "name", "y"));
{ name: 'y', value: 20 }

The code creates a list of objects, each with a name and a value. It then turns this list into a Set called mySet, which stores unique objects. The findObjectByKey function searches through the Set for an object that matches a specified key and value. It loops through each object in the Set, checking if the value of the given key matches the one we're looking for. If it finds a match, it returns that object; if not, it returns null.

Using an Array Conversion

The other simple way of searching for an object key in a Set is by converting the set into an array and then using array methods such as find().

Example

The following is a simple example for searching an object key in a set by converting it into an array and then using find() method.

const myObjects = [
    { name: "a", value: 0 }, 
    { name: "b", value: 1 }, 
    { name: "c", value: 2 }
];
const mySet = new Set(myObjects);

const result = Array.from(mySet).find(obj => obj.name === "a");
console.log(result);
{ name: 'a', value: 0 }

This code starts by creating an array called myObjects that contains three objects, each with a name and a value. It then creates a Set called mySet from this array, which stores the unique objects. To find the object with the name "a", the code converts the Set back into an array using Array.from(mySet) and uses the find method to search for the object where the name is "a".

Comparison

Method Performance Readability Flexibility
For...of Loop Better Moderate High
Array Conversion + find() Slower High Moderate

Conclusion

Both methods allow you to search for objects in a Set by their properties. The loop approach is more performant for large Sets, while array conversion offers cleaner, more readable code using built-in array methods.

Updated on: 2026-03-15T23:18:59+05:30

501 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements