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
Deep Search JSON Object JavaScript
Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures.
The Problem
Suppose we have the following nested JSON object:
const obj = {
id: 1,
title: 'hello world',
child: {
id: null,
title: 'foobar',
child: {
id: null,
title: 'i should be in results array'
}
},
foo: {
id: null,
title: 'i should be in results array too!'
},
deep: [
{
id: null,
value: 'yo'
}, {
id: null,
value: 'yo2'
}
]
};
We need to write a JavaScript function that takes an object, a key, and a value as parameters, then returns an array of all objects that contain the specified key-value pair.
Algorithm Approach
The solution uses a recursive approach with these steps:
- Check if the current item is a valid object; if not, return early
- If the current object has the matching key-value pair, add it to results
- Iterate through all properties and recursively search nested objects and arrays
- Return the collected array of matching objects
Implementation
const obj = {
id: 1,
title: 'hello world',
child: {
id: null,
title: 'foobar',
child: {
id: null,
title: 'i should be in results array'
}
},
foo: {
id: null,
title: 'i should be in results array too!'
},
deep: [
{
id: null,
value: 'yo'
}, {
id: null,
value: 'yo2'
}
]
};
const findObject = (obj = {}, key, value) => {
const result = [];
const recursiveSearch = (obj = {}) => {
if (!obj || typeof obj !== 'object') {
return;
}
if (obj[key] === value) {
result.push(obj);
}
Object.keys(obj).forEach(function (k) {
recursiveSearch(obj[k]);
});
};
recursiveSearch(obj);
return result;
};
console.log(findObject(obj, 'id', null));
[
{
id: null,
title: 'foobar',
child: {
id: null,
title: 'i should be in results array'
}
},
{
id: null,
title: 'i should be in results array'
},
{
id: null,
title: 'i should be in results array too!'
},
{
id: null,
value: 'yo'
},
{
id: null,
value: 'yo2'
}
]
How It Works
The function creates a nested recursiveSearch helper that:
- Base case: Returns early if the current item isn't an object
- Match check: Compares the object's property with the target key-value pair
- Recursion: Iterates through all object properties, calling itself on nested structures
- Collection: Maintains a results array in the outer function's scope
Alternative Implementation
Here's a more flexible version that can search for multiple criteria:
const deepSearchMultiple = (obj, criteria) => {
const results = [];
const search = (current) => {
if (!current || typeof current !== 'object') return;
// Check if current object matches all criteria
const matches = Object.keys(criteria).every(key =>
current[key] === criteria[key]
);
if (matches) {
results.push(current);
}
// Recursively search nested objects/arrays
Object.values(current).forEach(search);
};
search(obj);
return results;
};
// Example: Find objects with both id: null AND title containing "results"
const multiResults = deepSearchMultiple(obj, {id: null});
console.log(`Found ${multiResults.length} matching objects`);
Found 5 matching objects
Conclusion
Deep searching JSON objects uses recursion to traverse nested structures and collect matching elements. The key is handling both objects and arrays while maintaining a results collection across recursive calls.
