过滤循环的json仅返回变量匹配结果

时间:2018-07-08 18:27:57

标签: javascript json ajax

我正在尝试遍历json结果以仅输出_miner的匹配项。当前控制台仅输出“ []”,并且html为空白。我似乎无法弄清楚grep或for循环的哪一部分需要修改才能正确返回。

Json示例:

{
  "zelcash-114746": "0000000b195cc1419217007c01efcf1b6d90f16a215779b33b4ae06096b33b3d:942b928560a8e6231b0ee1e0a3db7b3ff67b4e5251de1880fe1830c418a3475d:114746:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Home:1531052142518",
  "zelcash-114481": "00000003ffbbcfde0b28f7f714909e18cd09de9c314207588a6e55a217a73c05:fbd33717c50b67fc3e84d94b46d758a464e81d0c8546ed5a887a046e495cee07:114481:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Lana:1531019942228",
  "zelcash-114423": "000000055a78937228c6a1f40e18c8553fab30e1f5ce84ff1fe47e27425d88fc:02043e03a090d31227e44dcabb03d4977d835531733c1684ec8112898b02f22a:114423:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Del:1531013991740",
  "zelcash-113970": "00000000838236ceb41b564f076b813b55afaebca93cda1b289e3698f84f5f2e:8176a2ae4077c08952b3802a18ee70a69d5407b1dcb090c6631dbb9ba709d1a7:113970:t1UWajgJUBdmzwgDvy9D2gyhHGq6dimbW2a.10603g1:1530959630994"
}

HTML

<p id="demo"></p>

JS / Ajax

$.ajax({
  url: "https://xxxxx/api/blocks",
  dataType: 'json',
  success: function (data) {
    var _miner='t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU';
    // find object
    for (var x in data) {
      var blockfound = $.grep(data, function(n,i) { 
        return n.x == _miner 
      });
      document.getElementById("demo").innerHTML += blockfound + "<br>";
    }

    console.log(blockfound);
  },
  error: function() {
    //alert("Was unable to get info!");
  }
});

2 个答案:

答案 0 :(得分:0)

使用for in遍历data并使用.includes()查找包含_miner的值:

const data = {
  "zelcash-114746": "0000000b195cc1419217007c01efcf1b6d90f16a215779b33b4ae06096b33b3d:942b928560a8e6231b0ee1e0a3db7b3ff67b4e5251de1880fe1830c418a3475d:114746:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Home:1531052142518",
  "zelcash-114481": "00000003ffbbcfde0b28f7f714909e18cd09de9c314207588a6e55a217a73c05:fbd33717c50b67fc3e84d94b46d758a464e81d0c8546ed5a887a046e495cee07:114481:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Lana:1531019942228",
  "zelcash-114423": "000000055a78937228c6a1f40e18c8553fab30e1f5ce84ff1fe47e27425d88fc:02043e03a090d31227e44dcabb03d4977d835531733c1684ec8112898b02f22a:114423:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Del:1531013991740",
  "zelcash-113970": "00000000838236ceb41b564f076b813b55afaebca93cda1b289e3698f84f5f2e:8176a2ae4077c08952b3802a18ee70a69d5407b1dcb090c6631dbb9ba709d1a7:113970:t1UWajgJUBdmzwgDvy9D2gyhHGq6dimbW2a.10603g1:1530959630994"
}

const _miner = 't1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU',
  result = [];

for (let i in data) {
  if (data[i].includes(_miner))
    result.push({
      [i]: data[i]
    })
}

result.forEach(e => {
  document.querySelector('#demo').innerHTML += Object.values(e)[0] + '<br />';
})
<p id="demo"></p>

您的代码应类似于:

$.ajax({
  url: "https://xxxxx/api/blocks",
  dataType: 'json',
  success: function(data) {

    var _miner = 't1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU', result = [];

    // find object

    for (let i in data) {
      if (data[i].includes(_miner))
        result.push({
          [i]: data[i]
        })
    }

    document.getElementById("demo").innerHTML += result + "<br>";

    console.log(result);

  },
  error: function() {
    //alert("Was unable to get info!");
  }
});

但是,document.getElementById("demo").innerHTML += result + "<br>";可能无法按预期工作,请使用循环显示result(如上面的代码段所示)

如果您想保留$.grep,则需要将值数组而不是对象传递给它,并像这样使用includes

var blockfound = $.grep(Object.values(data), function(n,i) { 
    return n.includes(_miner)
});
console.log(blockfound)

答案 1 :(得分:0)

您需要首先从data哈希中获取值,然后需要对其进行解析以获取miner字段(可以使用正则表达式来完成)。

var _miner='t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU';
var success = function (data) {
  var blockfound = [];
  var x;
  for(x in data) {
    var content = data[x];
    var s = content.match(/^([a-f0-9]+):([a-f0-9]+):(\d+):([A-Za-z0-9_-]+)\.([A-Za-z0-9]+):(\d+)$/);
    if (s && s[4]===_miner){
      document.getElementById("demo").innerHTML += content + "<br>";
      blockfound.push(content);
    }
  }

  console.log(blockfound);
}

success({
"zelcash-114746": "0000000b195cc1419217007c01efcf1b6d90f16a215779b33b4ae06096b33b3d:942b928560a8e6231b0ee1e0a3db7b3ff67b4e5251de1880fe1830c418a3475d:114746:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Home:1531052142518",
"zelcash-114481": "00000003ffbbcfde0b28f7f714909e18cd09de9c314207588a6e55a217a73c05:fbd33717c50b67fc3e84d94b46d758a464e81d0c8546ed5a887a046e495cee07:114481:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Lana:1531019942228",
"zelcash-114423": "000000055a78937228c6a1f40e18c8553fab30e1f5ce84ff1fe47e27425d88fc:02043e03a090d31227e44dcabb03d4977d835531733c1684ec8112898b02f22a:114423:t1QATR1DnhnrKEsv3tUvWzcjFHKEV4GPBZU.Del:1531013991740",
"zelcash-113970": "00000000838236ceb41b564f076b813b55afaebca93cda1b289e3698f84f5f2e:8176a2ae4077c08952b3802a18ee70a69d5407b1dcb090c6631dbb9ba709d1a7:113970:t1UWajgJUBdmzwgDvy9D2gyhHGq6dimbW2a.10603g1:1530959630994"
});
<p id="demo"></p>