我在 React 组件中成功获取了数据。
getData = () => {
fetch(`https://poloniex.com/public?command=returnChartData¤cyPair=BTC_XMR&end=9999999999&period=14400&start=1405699200`)
.then(res => res.json())
.then(results => this.setState({data1:results}))
.catch(e => e);
}
从 API 返回的数据如下所示
{
date: 1405699200,
high: 0.0047388,
low: 0.00408001,
open: 0.00504545,
close: 0.00435873,
volume: 47.34555992,
quoteVolume: 14311.88079097,
weightedAverage: 0.00430043
},
{
date: 1405699200,
high: 0.0045388,
low: 0.00403001,
open: 0.00404545,
close: 0.00435873,
volume: 44.34555992,
quoteVolume: 10311.88079097,
weightedAverage: 0.00430043
},
我只需要日期和结束数据,并且需要格式化而不需要像这样格式化的 key
{1405699200, 0.00435873}, {1405699200, 0.00534553}
我不知道该怎么做。我考虑过创建一个函数,在安装提取后将数据映射到新变量中,但这似乎无法正常工作。
最佳答案
你可以像这样使用 map
getData = () => {
fetch(`https://poloniex.com/public?command=returnChartData¤cyPair=BTC_XMR&end=9999999999&period=14400&start=1405699200`)
.then(res => res.json())
.then(results => {
this.setState({
data1:results.map(item => {
return [item.date, item.close]
})
})
})
.catch(e => e);
}
关于javascript - 从 FETCH API JSON 响应中删除字段和键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48525987/