[英]Removing Fields and Keys from FETCH API JSON response
I'm successfully fetching data like so in React component. 我已经成功地在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);
}
the data that is returned from the API looks like this 从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
},
I only need the date and close data and I need formatted without the key to be formatted like this 我只需要日期和结束数据,并且需要格式化而无需像这样格式化密钥
{1405699200, 0.00435873}, {1405699200, 0.00534553}
I'm not sure how to go about doing this. 我不确定该怎么做。 I thought about creating a function to map the data into a new variable after I mount the fetch, but this doesn't seem to be working properly.
我考虑过要在挂载获取数据后创建一个将数据映射到新变量的函数,但这似乎无法正常工作。
You could use map like this 您可以使用这样的地图
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.