Nodejs拼接JSON元素不能正常工作

时间:2014-07-05 12:43:52

标签: javascript json node.js splice

我有以下nodejs函数来检索api的数据(JSON)并尝试擦除所有0 Balance元素并将JSON发送到前端。

app.get('/getapibittrex', function(req, res){

  var url="https://bittrex.com/api/v1/account/getbalances?apikey="+req.query.apikey;                
                  request({
                    url: url,
                    json: true
                }, function (error, response, body) {

                    if (!error && response.statusCode === 200) {
                            var datos= body.result;

                            for ( var i = 0 ; i < datos.length; i++ ) {
                                if (datos[i].Balance==0)  {
                                    datos.splice(i,1);
                                }
                            }   

                            res.send(datos);
                    } else  { 
                    console.log("Error calling API: "+error);
                    }
                });

});

另一方面,当我调用此函数时,我获得了0余额的元素!我现在得到的是:

[
  {
    "Currency": "BTC",
    "Balance": 0.15080873,
    "Available": 0.143913061,
    "Pending": 0,
    "CryptoAddress": "1BtAz5AXbA4yvuSMneeSFnwdCGoHiSF6dA",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "BOST",
    "Balance": 0,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "BBdyTs52CmfX9MW1e8rYvoEFBvXByVYXaQ",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "VOOT",
    "Balance": 0,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "VDcdFniPZnfqT5jYVQsmqTfRXFpcGarKc5",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "CLOAK",
    "Balance": 561.25771864,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "C3SkQ7uv1kZU7ZnpdcvCT2NYpFAqrR2Qdm",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "LOL",
    "Balance": 0,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "LdTMw3czCvmmKJCSCgkC2sFsRPz91x7tPp",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "HPY",
    "Balance": 0,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "HS38GKGBKrraiNbyPHM1gmruCtA8N7B7PW",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "ATH",
    "Balance": 0,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "AY9P5Q3MhfsFG959UrEd6UV9nfVnmgjrBD",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "PTC",
    "Balance": 14467.59987795,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": null,
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "RZR",
    "Balance": 45.18269779,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": null,
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "KORE",
    "Balance": 4923.65705238,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": null,
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "TAC",
    "Balance": 4744.48127806,
    "Available": 4744.48127806,
    "Pending": 0,
    "CryptoAddress": "TcuQAW5QqCHwtpiGDU6uaox8eG3z3zvugZ",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "AERO",
    "Balance": 0,
    "Available": 0,
    "Pending": 0,
    "CryptoAddress": "APgKhLWQs1sdbgPHqs228nVdbH3B3xDBrM",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  },
  {
    "Currency": "SC",
    "Balance": 1620.08759994,
    "Available": 336.88682308,
    "Pending": 0,
    "CryptoAddress": "BFUWs4gnZpB8Qb7RoiYjnst1iSqHCRC6Ec",
    "Requested": false,
    "Uuid": "4662193c-2ca1-4729-beb8-85a373a9879e"
  }
]

一定有我遗失的东西...... 的问候,

1 个答案:

答案 0 :(得分:3)

您可以使用Array.prototype.filter并且不要重新发明轮子。

var datos = body.result.filter(function(data){
    return !!data.Balance;
});