hbi99 / defiant.js

http://defiantjs.com
GNU Affero General Public License v3.0
913 stars 91 forks source link

How to pass a dynamic variable JSON.Search function #81

Closed punit07 closed 7 years ago

punit07 commented 7 years ago

Hi there,

I have a form, where user enter a string/keyword to query. I want to pass this keyword to this function ... I have tried something like this ....

var cname = $("#name").val();
var reds = JSON.search(myjson, '//*[contains(courseName, cname)]');

but its not working.

Can you please advise how do I pass a dynamic variable to the JSON.search function ?

Thanks

hakan-peltarion commented 7 years ago

Hi Punit, Sorry for late response...I've been focusing coding on AI and Deep Learning - thus shutting off everything else :-S

You can do something like this:

var cname = $("#name").val();
var reds = JSON.search(myjson, '//*[contains(courseName, "'+ cname +'")]');

To gain even better performance, I'd suggest that you create a snapshot of your JSON and use the snapshot when searching. Using snapshot, you'll see huge performance gain, especially if you have large JSON data.

Cheers