返回Json for循环并过滤结果

时间:2018-07-15 05:03:02

标签: javascript json ajax

我试图遍历JSON数据以显示“时间”,“块”的结果,并将“数量”过滤为_miner的特定变量匹配。

当前,我能够返回名称,时间和块,但是我似乎无法弄清楚如何正确地将最终金额过滤到特定的地址。完成后,我将把此列表输出到一个表中,以向特定用户显示结果。

[
   {
      "name":"zelcash",
      "pending":{
         "blocks":[

         ],
         "confirms":{

         }
      },
      "payments":[
         {
            "time":1531601680340,
            "txid":"de12e0a3a0d75df7f9d5da6e78896ec5f6d72c0063df8d059b90d66a6aa0d535",
            "shares":3386.04739737,
            "paid":149.85034477,
            "miners":2,
            "blocks":[
               119148
            ],
            "amounts":{
               "t1XHpNtYY2N3EMDRoX9RM2hq4DWWPZSmawJ":94.69254759,
               "t1ZsrJASHjvSU8mcLA4K1KuBYESMpXQgwpM":55.15779718
            },
            "balances":{

            },
            "work":{
               "t1XHpNtYY2N3EMDRoX9RM2hq4DWWPZSmawJ":2139.69113513,
               "t1ZsrJASHjvSU8mcLA4K1KuBYESMpXQgwpM":1246.35626224
            }
         },
         {
            "time":1531599760228,
            "txid":"6e11fa783146fab54ec3f9160cd41411bdd3bc68654bbdc73bc1374940e73d06",
            "shares":9932.46660094,
            "paid":149.85040001,
            "miners":2,
            "blocks":[
               119136
            ],
            "amounts":{
               "t1XHpNtYY2N3EMDRoX9RM2hq4DWWPZSmawJ":94.33175716,
               "t1ZsrJASHjvSU8mcLA4K1KuBYESMpXQgwpM":55.51864285
            },
            "balances":{

            },
            "work":{
               "t1XHpNtYY2N3EMDRoX9RM2hq4DWWPZSmawJ":6252.54939124,
               "t1ZsrJASHjvSU8mcLA4K1KuBYESMpXQgwpM":3679.9172097
            }
         },
             //More returned content
      ]
   }
]

Ajax / JS

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

        var _miner = "t1XHpNtYY2N3EMDRoX9RM2hq4DWWPZSmawJ";

        var addr = _miner;

        for (var i = 0; i < data.length; i++) {

            //Name
            console.log(data[i].name);

            var payments = data[i].payments;
            for (var j = 0; j < payments.length; j++) {

                //Time
              //console.log(payments[j].time);

                //blocks
              var blocks = payments[j].blocks;
              for (var b = 0; b < blocks.length; b++) {
                //console.log(blocks[b])
              }

              console.log(payments[j].amounts);


            }
        }

    },

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

我试图让控制台返回以下内容,以便将循环输出到表中

zelcash
1531601680340
94.69254759
zelcash
1531599760228
94.33175716
//repeat

1 个答案:

答案 0 :(得分:0)

您的描述不明确。我认为这可能是您的答案。请检查并确认它。

    for (var i = 0; i < data.length; i++) {
        var payments = data[i].payments;
        for (var j = 0; j < payments.length; j++) {
          //Name
          console.log(data[i].name);

          //Time
          console.log(payments[j].time);

          // amounts
          if (payments[j].amounts[_miner] !== 'undefined')
            console.log(payments[j].amounts[_miner])
        }
    }