DEV Community

Cover image for How To Create A Global "JSON Search Algorithm" In JavaScript.

How To Create A Global "JSON Search Algorithm" In JavaScript.

Lucius Emmanuel Emmaccen on March 01, 2021

Writing a Search feature for every single page or section on your website can be really tedious, and even if you create a reusable function, you mi...
Collapse
 
haidarknightfury profile image
Haidar

What if you have nested objects?

Collapse
 
emmaccen profile image
Lucius Emmanuel Emmaccen

Good question, in that case, another search would have to be implemented on the nested object, something like: topLevelData.nestedObject and this would pass for our search array.
Hope I've answered your question Haidar?

Collapse
 
abdheshnayak profile image
Abdhesh Nayak • Edited

For the similar task I i tried to solve it by considering the json object as a string and then traversing the cursor position for each search result.

Try here
d2x8qs.csb.app/

Source code: github.com/abdheshnayak/search-in-...

Thread Thread
 
isocroft profile image
Okechukwu Ifeora

Hey @abdheshnayak , you can use a much more comprehensive function

useTextFilteredList(). See source code here: github.com/codesplinta/busser/blob...

See it in action here: codesandbox.io/p/sandbox/react-bus...

Let me know what you think

Collapse
 
haidarknightfury profile image
Haidar

Yes but not really scalable. What if you have more than 3 level of nesting. I think you could improve your function a bit

Thread Thread
 
johnkazer profile image
John Kazer

There's a library called fusejs that flattens out objects with nested arrays. It checks the type of each value and if array recurses. All in order to create one big flat object you can search in.

Thread Thread
 
emmaccen profile image
Lucius Emmanuel Emmaccen

Thanks for sharing John.

Thread Thread
 
johnkazer profile image
John Kazer

You might find fusejs interesting as a search library - the "flattening" process is part of how it searches rather than main function.

Collapse
 
ianwijma profile image
Ian Wijma

You do object.values on the json items. Then do stringify on the item values. Than call includes on that string.

Not sure about performance though.

Collapse
 
isocroft profile image
Okechukwu Ifeora • Edited

This is a nice article. A really great reusable search function. I really like it. Cool stuff.

I just have some questions.

Wouldn't it be better to just return the results from the function instead of passing a setter function to the search function ?

Also, the arrangement of your argument list for the search function, why not arrange optional arguments as the last two of the four arguments while the non-optional arguments be the first 2 ?

Check out: useTextFilteredList() defined (source code) here: github.com/codesplinta/busser/blob...

See it in action: codesandbox.io/p/sandbox/react-bus...

Let me know what you think ?

Collapse
 
abdheshnayak profile image
Abdhesh Nayak • Edited

For the similar task I i tried to solve it by considering the json object as a string and then traversing the cursor position for each search result.

Try here
d2x8qs.csb.app/

Source code: github.com/abdheshnayak/search-in-...