-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_utils.js
57 lines (48 loc) · 1.73 KB
/
dev_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Useful functions for use in the browser console for development.
*/
function bfsSearchNestedObject(obj, searchString, maxDepth = 1000) {
// Initialize the queue with the root object, starting at depth 0 and an empty path
const queue = [{ currentObj: obj, path: [], depth: 0 }]
// Process each item in the queue until it's empty
while (queue.length > 0) {
const { currentObj, path, depth } = queue.shift()
// Stop the search if the max depth is exceeded
if (depth > maxDepth) {
return null
}
// Iterate over each key in the current object
for (let key in currentObj) {
const value = currentObj[key]
const currentPath = path.concat(key) // Update the path
// If the value is a string and matches the search string, return the path
if (typeof value === 'string' && value === searchString) {
return currentPath
}
// If the value is an object, add it to the queue for further exploration
if (typeof value === 'object' && value !== null) {
queue.push({
currentObj: value,
path: currentPath,
depth: depth + 1,
})
}
}
}
// If the string is not found, return null
return null
}
// Example usage:
const data = {
a: {
b: {
c: 'hello',
d: {
e: 'world',
},
},
},
}
console.log(bfsSearchNestedObject(data, 'hello')) // Output: ['a', 'b', 'c']
console.log(bfsSearchNestedObject(data, 'world')) // Output: ['a', 'b', 'd', 'e']
console.log(bfsSearchNestedObject(data, 'not found')) // Output: null