+ 1

hey guys, can somebody help me with this algorithm in javascript

I need to make a search in this json, it's dynamic so can have lots folders and I need to look through all of them. The search should work the next way, it should go through all the folders (as I said the number of folders can change) and look for "File2", for example, and indicate its file id. ( for the case of "File2" there is two files with this name and have ids 17 and 15). { "id": 1, "name": "Main", "children": [ { "id": 16, "parentId": 1, "name": "Folder1", "children": [], "files": [ { "id": 14, "name": "File1" }, { "id": 15, "name": "File2" }, { "id": 16, "name": "File3" } ] }, { "id": 2, "parentId": 1, "name": "Folder2", "children": [ { "id": 86, "parentId": 2, "name": "Folder2", "children": [], "files": [ { "id": 17, "name": "File2" }, { "id": 18, "name": "File5" } ] } ], "files": [] } ], "files": [ { "id": 21, "name": "File9" } ] }

9th Jul 2021, 2:31 PM
Diana Rodriguez
Diana Rodriguez - avatar
3 Answers
+ 1
have you tried to implement it? by using recursive function, it would not be such hard as it may look at first ^^ https://code.sololearn.com/W3Y5xh09B82r/?ref=app you can paste your own valid json in the textarea... however, I've made the search to be exactly the file name (case sensitive), but making it case insensitive and/or searching only if substring match would not be hard to modify ;P
9th Jul 2021, 8:32 PM
visph
visph - avatar
+ 1
visph, rudolph flash thank you so much guys! great solutions and so clean, I loved them!
9th Jul 2021, 11:01 PM
Diana Rodriguez
Diana Rodriguez - avatar