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
Search a complex object by id property in JavaScript
Suppose we have a complex JSON Object like this ?
const obj = {
"id": "0001",
"fieldName": "sample1",
"fieldValue": "0001",
"subList": [
{
"id": 1001,
"fieldName": "Sample Child 1",
"fieldValue": "1001",
"subList": []
},
{
"id": 1002,
"fieldName": "Sample Child 2",
"fieldValue": "1002",
"subList": []
}
]
}
We are required to write a JavaScript function that takes in one such object and a key value pair (necessarily an "id" key-value pair). Then the function should return the whole sub object that contains the queried key/value pair.
Using Recursive Search
The most effective approach is to use recursion to traverse the nested structure and search for the matching id:
const obj = {
"id": "0001",
"fieldName": "sample1",
"fieldValue": "0001",
"subList": [
{
"id": 1001,
"fieldName": "Sample Child 1",
"fieldValue": "1001",
"subList": []
},
{
"id": 1002,
"fieldName": "Sample Child 2",
"fieldValue": "1002",
"subList": []
}
]
}
function searchById(searchKey, obj) {
let key = Object.keys(searchKey)[0];
let res;
if (obj[key] === searchKey[key]) {
return obj;
}
if (obj.subList && obj.subList.length > 0) {
obj.subList.some(function (a) {
res = searchById(searchKey, a);
return res;
});
}
return res;
}
console.log(searchById({id: 1002}, obj));
{
id: 1002,
fieldName: 'Sample Child 2',
fieldValue: '1002',
subList: []
}
Alternative Approach Using find()
Here's a more modern approach using Array methods:
function findObjectById(id, obj) {
if (obj.id === id) {
return obj;
}
if (obj.subList) {
for (let child of obj.subList) {
const found = findObjectById(id, child);
if (found) return found;
}
}
return null;
}
// Test the function
console.log(findObjectById(1001, obj));
console.log(findObjectById("0001", obj));
{
id: 1001,
fieldName: 'Sample Child 1',
fieldValue: '1001',
subList: []
}
{
id: '0001',
fieldName: 'sample1',
fieldValue: '0001',
subList: [ { id: 1001, fieldName: 'Sample Child 1', fieldValue: '1001', subList: [] }, { id: 1002, fieldName: 'Sample Child 2', fieldValue: '1002', subList: [] } ]
}
How It Works
The recursive search algorithm works by:
- Checking if the current object matches the search criteria
- If not, recursively searching through each item in the
subListarray - Using
some()method to stop as soon as a match is found - Returning the matching object or
undefinedif not found
Key Points
- Always check if
subListexists and has items before iterating - The
some()method stops iteration when it finds a truthy return value - Recursive functions need proper base cases to avoid infinite loops
- Handle both string and numeric id types as shown in the examples
Conclusion
Recursive search is the most effective way to find objects by id in nested structures. The algorithm traverses the entire tree until it finds the matching object, making it suitable for complex nested JSON data.
Advertisements
