Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Searching objects by value in JavaScript
When working with complex JavaScript objects, you often need to find all keys that contain a specific value. This is particularly useful for nested objects where the same value might appear at multiple levels.
Consider this example object with nested structure:
const obj = {
"name": "Vivek Sharma",
"occupation": "Software Engineer",
"age": 23,
"contacts": [{
"name": "Mukul Sharma",
"occupation": "Software Engineer",
"age": 31,
}, {
"name": "Jay Sharma",
"occupation": "Software Engineer",
"age": 27,
}, {
"name": "Rajan Sharma",
"occupation": "Software Engineer",
"age": 32,
}]
};
console.log("Original object:", JSON.stringify(obj, null, 2));
Original object: {
"name": "Vivek Sharma",
"occupation": "Software Engineer",
"age": 23,
"contacts": [
{
"name": "Mukul Sharma",
"occupation": "Software Engineer",
"age": 31
},
{
"name": "Jay Sharma",
"occupation": "Software Engineer",
"age": 27
},
{
"name": "Rajan Sharma",
"occupation": "Software Engineer",
"age": 32
}
]
}
Creating the keysOf Method
We'll create a prototype method that searches through nested objects and returns all keys containing a specific value:
const keysOf = function(val, obj = this, res = []) {
const keys = Object.keys(obj);
for (let ind = 0; ind
Example Usage
const obj = {
"name": "Vivek Sharma",
"occupation": "Software Engineer",
"age": 23,
"contacts": [{
"name": "Mukul Sharma",
"occupation": "Software Engineer",
"age": 31,
}, {
"name": "Jay Sharma",
"occupation": "Software Engineer",
"age": 27,
}, {
"name": "Rajan Sharma",
"occupation": "Software Engineer",
"age": 32,
}]
};
const keysOf = function(val, obj = this, res = []) {
const keys = Object.keys(obj);
for (let ind = 0; ind
Keys with value 23: ['age']
Keys with value 'Software Engineer': ['occupation']
Keys with value 'Vivek Sharma': ['name']
How It Works
The algorithm uses recursive traversal:
- Base case: If a value matches exactly, add the key to results
- Recursive case: If the value is a non-null, non-array object, search deeper
-
Accumulator: The
resarray collects all matching keys across all levels
Alternative Implementation
Here's a standalone function approach without modifying the prototype:
function findKeysByValue(obj, searchValue, currentPath = []) {
const results = [];
for (const [key, value] of Object.entries(obj)) {
if (value === searchValue) {
results.push(key);
} else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
results.push(...findKeysByValue(value, searchValue, [...currentPath, key]));
}
}
return results;
}
const testObj = {
name: "John",
age: 30,
details: {
age: 30,
city: "New York"
}
};
console.log("Keys with value 30:", findKeysByValue(testObj, 30));
console.log("Keys with value 'John':", findKeysByValue(testObj, "John"));
Keys with value 30: ['age', 'age'] Keys with value 'John': ['name']
Conclusion
Searching objects by value requires recursive traversal for nested structures. The keysOf method provides an efficient way to find all keys containing a specific value across multiple object levels.
Advertisements
