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

时间:2022-12-15 21:27:17

I've got the following nodejs function that retrieves an api's data (JSON) and tries to erase all the 0 Balance elements and send the JSON to frontend.

我有以下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);
                    }
                });

});

On the other hand when i call this function i get elements with 0 balance!! What i am getting now is:

另一方面,当我调用此函数时,我获得了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"
  }
]

There must be something that i am missing.... Regards,

必须有一些我缺少的东西....问候,

1 个解决方案

#1


3  

You can use Array.prototype.filter and do not reinvent the wheel.

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

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

#1


3  

You can use Array.prototype.filter and do not reinvent the wheel.

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

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