我正在尝试构建一个简单的CLI加密货币跟踪器应用程序。该应用程序执行了成功的API调用,并返回以下响应:
[ { exchange: 'binance',
base: 'ADA',
quote: 'BTC',
price_quote: '0.00001663',
timestamp: '2019-04-08T16:36:00Z' },
{ exchange: 'binance',
base: 'ADX',
quote: 'BTC',
price_quote: '0.00003316',
timestamp: '2019-04-08T16:35:00Z' },
...]
如何访问响应中的特定对象?例如,如何将整个对象返回到base: 'ADA
所在的地方?
这是返回JSON响应的简单Axios调用:
axios.get("https://api.nomics.com/v1/exchange-markets/prices?key=" + apiKey + "¤cy=BTC&exchange=binance")
.then(function (response) {
console.log(response.data)
})
答案 0 :(得分:0)
您可以使用find
let response = [ { exchange: 'binance',base: 'ADA',quote: 'BTC',price_quote: '0.00001663',timestamp: '2019-04-08T16:36:00Z' },
{exchange: 'binance', base: 'ADX',quote: 'BTC',price_quote: '0.00003316',timestamp: '2019-04-08T16:35:00Z' },]
let value = response.find(e => e.base === 'ADA')
console.log(value)
答案 1 :(得分:0)
axios.get("https://api.nomics.com/v1/exchange-markets/prices?key=" + apiKey + "¤cy=BTC&exchange=binance")
.then(function (response) {
console.log(response.data.find(data=>data.base==='ADA'))
}
“查找”功能将遍历数组的每个项目,直到找到与提供的布尔条件匹配的项目并返回它。