Imagine I had a local JSON file that looked like this:
var institutes ={"colleges":[{"name":"Massachusetts Institute of Technology","address":{"line1":"77 Massachusetts Ave","City":"Cambridge","state":"Massachusetts","zip":"02139"},"phone":"(617) 253-2139","courses":["Computer Science","Mathematics","Physics"]},{"name":"Boston College","address":{"line1":"140 Commonwealth Drive","City":"Chestnut Hill","state":"Massachusetts","zip":"(617) 552-8000"},"phone":"(617) 253-8000","courses":["Law","Nursing","Theology"]},{"name":"UC Berkeley College","address":{"line1":"320 Mclaughlin Hall","City":"Berkeley","state":"California","zip":"92139"},"phone":"(510) 642-5771","courses":["Computer Science","Business Administration","Theology"]}]};
How would I write a search function that could potentially traverse through the entirety, looking at various keys to grab a query perimeter and return the most relevant objects using JavaScript only? For example, if my function were called search() and I perform the following call:
search("mass");
It would return me something like the following:
{"search results":{"entities_found":2,"highlights":4},"colleges":[{"name":"<span class='highlight'>Mass</span>achusetts Institute of Technology","address":{"line1":"77 <span class='highlight'>Massa</span>chusetts Ave","City":"Cambridge","state":"<span class='highlight'>Mass</span>achusetts","zip":"02139"},"phone":"(617) 253-2139","courses":["Computer Science","Mathematics","Physics"]},{"name":"Boston College","address":{"line1":"140 Commonwealth Drive","City":"Chestnut Hill","state":"<span class='highlight'>Mass</span>achusetts","zip":"(617) 552-8000"},"phone":"(617) 253-8000","courses":["Law","Nursing","Theology"]}};
Question
Brian Miller
Imagine I had a local JSON file that looked like this:
How would I write a search function that could potentially traverse through the entirety, looking at various keys to grab a query perimeter and return the most relevant objects using JavaScript only? For example, if my function were called search() and I perform the following call:
It would return me something like the following:
Equally, if I were to search for the following:
I would get this:
Link to comment
https://www.neowin.net/forum/topic/1435030-js-searching-thought-local-json-file/Share on other sites
6 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now