Javascript 从FETCH API JSON响应中删除字段和键
我在React组件中成功地获取了这样的数据Javascript 从FETCH API JSON响应中删除字段和键,javascript,json,reactjs,fetch,Javascript,Json,Reactjs,Fetch,我在React组件中成功地获取了这样的数据 getData = () => { fetch(`https://poloniex.com/public?command=returnChartData¤cyPair=BTC_XMR&end=9999999999&period=14400&start=1405699200`) .then(res => res.json()) .then(results =>
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
},
{1405699200, 0.00435873}, {1405699200, 0.00534553}
我只需要日期和关闭数据,我需要格式化,没有键,格式如下
{
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
},
{1405699200, 0.00435873}, {1405699200, 0.00534553}
我不知道该怎么做。我曾考虑在装载fetch之后创建一个函数来将数据映射到一个新变量中,但这似乎不能正常工作 你可以像这样使用地图
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);
}
您的数据格式只是没有键的值..哪个是无效的javascriptso使用数组<代码>[[1405699200,0.00435873],[1405699200,0.00534553]-在每个el中,第一个项目是体积,第二个是平均值。大多数图表都支持x轴和y轴。您的意思是需要将其格式化为字符串吗?这不是一个有效的JS数据结构。如果需要数组数组,只需执行
results.map(r=>[r.date,r.close])
是的,很抱歉应该说数组数组…map方法似乎以正确的格式获取数据(或者我认为是这样),但我的highcharts图没有加载数据。。。