简体   繁体   English

过滤 JSON API 响应

[英]Filter JSON API response

I'm using node-binance-api to pull some Binance balance data.我正在使用node-binance-api来提取一些 Binance 余额数据。 The API call returns all of the currencies, and I want to filter it to only include those where I have a balance. API 调用返回所有货币,我想对其进行过滤以仅包括我有余额的货币。

Code:代码:

  await binance.balance((error, balances) => {
    if ( error ) return console.error(error);
    console.log(balances);
    var filtered = _.filter(balances, function(b) {return b.available > 0})
    console.log(filtered);
  });

The first console.log returns all data as expected:第一个console.log按预期返回所有数据:

  RENBTC: { available: '0.00000000', onOrder: '0.00000000' },   
  SLP: { available: '0.00000000', onOrder: '0.00000000' },   
  STRAX: { available: '0.00000000', onOrder: '0.00000000' },   
  ...

I expected the second console.log to return the coin name and the balance, but it's only returning the balances:我希望第二个console.log返回硬币名称和余额,但它只返回余额:

[
  { available: '0.03993250', onOrder: '0.00000000' },
  { available: '8.11227680', onOrder: '0.00000000' }
]

How can I get the coin symbol to be included after filtering?如何在过滤后包含硬币符号?

Lodash's _.filter , even when passed an object, returns an array. Lodash 的_.filter ,即使通过了 object,也会返回一个数组。

_.filter _。筛选

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.遍历集合的元素,返回一个包含所有元素的数组,谓词返回真值。

If you want an object of the same format but "filtered" one way would be to iterate over the keys of the object, test that properties value, then assign or not to a new object.如果您想要相同格式但“过滤”的 object,一种方法是遍历 object 的键,测试该属性值,然后分配或不分配给新的 object。

 const balances = { RENBTC: { available: '0.00000000', onOrder: '0.00000000' }, SLP: { available: '0.00000000', onOrder: '0.00000000' }, STRAX: { available: '0.00000000', onOrder: '0.00000000' }, XXX: { available: '0.03993250', onOrder: '0.00000000' }, YYY: { available: '8.11227680', onOrder: '0.00000000' } }; const filtered = {}; for (const k of Object.keys(balances)) { if (balances[k].available > 0) { filtered[k] = balances[k]; } } console.dir(filtered);

An alternate way would be to delete the properties you don't want in the object, but this would be destructive if you don't copy the object first.另一种方法是删除object 中不需要的属性,但如果不先复制 object,这将具有破坏性。

If you still want to get an array of the following type, you can use reduce如果还想得到如下类型的数组,可以使用reduce

[
  {
    "available": "0.03993250",
    "key": "XXX",
    "onOrder": "0.00000000"
  },
  {
    "available": "8.11227680",
    "key": "YYY",
    "onOrder": "0.00000000"
  }
]

 const balances = { RENBTC: { available: '0.00000000', onOrder: '0.00000000' }, SLP: { available: '0.00000000', onOrder: '0.00000000' }, STRAX: { available: '0.00000000', onOrder: '0.00000000' }, XXX: { available: '0.03993250', onOrder: '0.00000000' }, YYY: { available: '8.11227680', onOrder: '0.00000000' } }; const filtered = Object.entries(balances).reduce((acc, [key, value]) => { const available = value.available if (available <= 0) { return acc } acc.push({ available, key, onOrder: value.onOrder }) return acc }, []) console.log("Result", filtered);

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

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