简体   繁体   English

使用jQuery过滤JSON结果

[英]Filter JSON results with jQuery

There is a JSON search engine here . 有一个JSON的搜索引擎这里 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: 我试图找出如何过滤谷歌表的结果,只包括字符小于4和大于7的项目,例如:

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

So in jQuery I did this: 所以在jQuery中我这样做了:

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. 然后我假设它必须进入一个if语句(根据注释中的建议)但if语句已经填满了我把它放在一行后面说data.feed.entry.forEach(function(entry) (第19行data.feed.entry.forEach(function(entry)下面)并在该行之前进行了测试。

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. 我尝试将入口过滤器放在第150行(注意GitHub项目),但它没有发现任何错误并且无法正常工作。 Am I perhaps trying to put this in the wrong section? 我可能试图把它放在错误的部分吗? I figured it should go right after the ajax call. 我认为应该在ajax调用之后立即进行。 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. 我可以这样做是简单的jQuery,但是当我在Ajax中将它结合起来时,它让我感到茫然。 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. 任何帮助,将不胜感激。

Found it! 找到了!

In line 76-80 of the original you can change the var matches"whatever" and filter the results. 在原始的第76-80行中,您可以更改var匹配“whatever”并过滤结果。 It will still import all results but it will adjust to remove the results carrying the string you specify upon imputing search terms. 它仍会导入所有结果,但会调整以删除带有您在输入搜索条件时指定的字符串的结果。 Eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM