javascript - 从 FETCH API JSON 响应中删除字段和键

标签 javascript json reactjs fetch

我在 React 组件中成功获取了数据。

getData = () => {
     fetch(`https://poloniex.com/public?command=returnChartData&currencyPair=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&currencyPair=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/

相关文章:

JavaScript promise : Reject handler vs catch

javascript - Discord JS//尝试通过对消息使用react来添加角色

javascript - 基于对象数组中不同单位的总和值

javascript - Typescript - null 和 undefined 之间有什么区别?

asp.net-mvc - 如何在 MVC 3 中显示 JSON 图像?

javascript - 是否可以创建嵌入式 React 应用程序小部件?

javascript - 当URL不匹配任何路由时以及当URL包含/#/使用ReactJS时如何显示404

javascript - 在不导入的情况下访问 React 中的 JSON 文件

c# - 如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符串?

reactjs - 如何在react-native中setState ComponentDidUpdate中的props值?