Filter JSON results with jQuery

Viewed 410

There is a JSON search engine here. I am trying to figure out how I can filter the results of a google sheet to just include items with characters of less than 4 and more than 7, for example:

entry.filter(entry => entry.gsx$status.$t.length > 7 || entry.gsx$status.$t.length < 4)

So in jQuery I did this:

mounted: function () {
     var entry = data.feed.entry;
     var result = entry.filter(entry => entry.gsx$status.$t.length > 7 || entry.gsx$status.$t.length < 4); 
$.ajax({
      dataType: "json",
      url: GSHEET_JSON_URL + '&cache-buster=' + Math.random(),
      success: function(data, result) {
            if (data && data.feed && data.feed.entry && data.feed.entry[0]) {.....etc....

Then I assumed that it had to go into an if statement (as per suggestion in comments) but the if statements are already so filled up I put it after line that says data.feed.entry.forEach(function(entry)(line 19 below) and also tested it before that line.

My problem is that I cannot quite figure out where to put it in the code listed. I tried putting the entry filter in at line 150 (note the GitHub project) but it shoots back no errors and doesn't work. Am I perhaps trying to put this in the wrong section? I figured it should go right after the ajax call. Eg in this section:

  mounted: function () {
             $.ajax({
          dataType: "json",
          url: GSHEET_JSON_URL + '&cache-buster=' + Math.random(),
                   success: function(data) {
            if (data && data.feed && data.feed.entry && data.feed.entry[0         var missing = ['customer', 'regisnbrtx', 'num', 'description', 'qty', 'price', 'total', 'address', 'status'].filter(function(name) {
                return !data.feed.entry[0]['gsx$' + name];
              });
              if (missing[0]) {
                vueSearch.error = 'You must add the "`' + missing.join('`", "`').replace(/, (?!.*,)/, ', and ') + '`" field' + (missing[1] ? 's' : '') + '.';
                return;
              }
            }
            else {
              vueSearch.error = 'The specified GSHEET_JSON_URL is invalid:<br>`' + GSHEET_JSON_URL + '`';
              return;
            }

            data.feed.entry.forEach(function(entry) {

              vueSearch.links.push({
                customer: entry.gsx$customer.$t, 
                regisnbrtx: entry.gsx$regisnbrtx.$t,
                num: entry.gsx$num.$t,
                description: entry.gsx$description.$t,
                qty: entry.gsx$qty.$t,
                price: entry.gsx$price.$t,
                total: entry.gsx$total.$t,
                address: entry.gsx$address.$t,
                status: entry.gsx$status.$t,
                partrecieved: entry.gsx$partrecieved.$t,
                date: entry.gsx$date.$t,
                shipdate: entry.gsx$shipdate.$t,
                itemrec: entry.gsx$itemrec.$t,
                comments: entry.gsx$comments.$t
              });
            });

            vueSearch.loading = false;
            vueSearch.updateResults();
          },
          error: function() {
            vueSearch.error = 'The specified GSHEET_JSON_URL does not contain JSON:<br>`' + GSHEET_JSON_URL + '`';
            vueSearch.loading = false;
          }
        });
        // Set focus to search box
        $('#barcoder').select();
      }
    });
  });

I can do this is plain jQuery, but when I combine it in Ajax it leaves me at a loss. There are several posts here on filtering, but not by character length from what I've found.

Also tried to contact the creator and post on their page but no luck. Any help would be appreciated.

1 Answers

Found it!

In line 76-80 of the original you can change the var matches"whatever" and filter the results. It will still import all results but it will adjust to remove the results carrying the string you specify upon imputing search terms. E.g:

var results = self.links
            .reduce(function(scores, link, linkIndex) {
              var score = searchTerms.reduce(function(score, searchTerm) {
                if (score !== false) {

     //modify one of these below, my example on the first var:

                  var matchesTitle = ((link.title).match('YOURSTRINGHERE'));
                  var matchesURL = (link.url.match(searchTerm.rgx) || '').length;
                  var matchesDescr = ((link.description || '').match(searchTerm.rgx) || '').length;
                  var matchesKeywords = ((link.keywords || '').match(searchTerm.rgx) || '').length;
                  var matchesAny = !!(matchesTitle || matchesURL || matchesDescr || matchesKeywords);
                  return searchTerm.must !== !matchesAny
Related