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
Find specific key value in array of objects using JavaScript
When working with JavaScript objects containing arrays of nested objects, you often need to find which parent key contains an object with specific property values. This is common when dealing with product catalogs, user groups, or categorized data.
Example Data Structure
Consider this product catalog object where each category contains an array of products:
const obj = {
"LAPTOP": [{
"productId": "123"
}],
"DESKTOP": [{
"productId": "456"
}],
"MOUSE": [{
"productId": "789"
}, {
"productId": "012"
}],
"KEY-BOARD": [{
"productId": "345"
}]
};
Method 1: Using Object.keys() and Array.find()
This approach iterates through all keys and uses find() to locate the matching product:
const obj = {
"LAPTOP": [{
"productId": "123"
}],
"DESKTOP": [{
"productId": "456"
}],
"MOUSE": [{
"productId": "789"
}, {
"productId": "012"
}],
"KEY-BOARD": [{
"productId": "345"
}]
};
const searchByPair = (obj = {}, pair = {}) => {
const toSearch = Object.values(pair)[0];
let required = undefined;
Object.keys(obj).forEach((key) => {
if(obj[key].find((product) => product.productId === toSearch)){
required = key;
}
});
return required;
};
console.log(searchByPair(obj, {'productId': '123'}));
console.log(searchByPair(obj, {'productId': '789'}));
console.log(searchByPair(obj, {'productId': '999'})); // Not found
LAPTOP MOUSE undefined
Method 2: Using for...in Loop (More Efficient)
This approach stops searching once the first match is found, making it more efficient:
const searchByPairOptimized = (obj = {}, pair = {}) => {
const [key, value] = Object.entries(pair)[0];
for (const category in obj) {
if (obj[category].some(product => product[key] === value)) {
return category;
}
}
return undefined;
};
console.log(searchByPairOptimized(obj, {'productId': '456'}));
console.log(searchByPairOptimized(obj, {'productId': '012'}));
DESKTOP MOUSE
Method 3: Generic Search Function
A more flexible function that can search by any property:
const findCategoryByProperty = (data, searchCriteria) => {
const [property, targetValue] = Object.entries(searchCriteria)[0];
for (const [category, items] of Object.entries(data)) {
if (items.some(item => item[property] === targetValue)) {
return category;
}
}
return null;
};
// Test with different products
console.log(findCategoryByProperty(obj, {productId: '345'}));
console.log(findCategoryByProperty(obj, {productId: '789'}));
KEY-BOARD MOUSE
Comparison of Methods
| Method | Performance | Readability | Stops on First Match |
|---|---|---|---|
| forEach + find | Slower | Good | No |
| for...in + some | Faster | Good | Yes |
| Generic function | Faster | Best | Yes |
Conclusion
Use the for...in loop with Array.some() for optimal performance when searching nested object arrays. The generic approach offers the best flexibility for different search criteria.
