【问题标题】:Access Nested Object - ReactJs访问嵌套对象 - ReactJs
【发布时间】:2020-03-28 20:12:01
【问题描述】:

我正在尝试解析和映射嵌套对象,如下所示:

{
  "country": "germany",
  "timeline": {
    "cases": {
      "1/22/20": 0,
      "1/23/20": 0,
      "1/24/20": 0,
      "1/25/20": 0,
      "1/26/20": 0,
      "1/27/20": 1,
      "1/28/20": 4,
      "1/29/20": 4,
      "1/30/20": 4
    }
  }
}

这是我的第一次尝试,我尝试了几种在组件中插入响应的变体。

import React, { Component } from 'react'

class Chart extends Component {
  state = {
    historyStats: [],
    // error: true
  };

  componentDidMount() {
    fetch('https://corona.lmao.ninja/v2/historical/germany')
    .then(res => res.json())
    .then((data) => {
      this.setState({ historyStats: data })
    })
    .catch(console.log)
  }

  render() {
    const {historyStats} = this.state;
    console.log(Object.values(historyStats));

    return (
      <div>

       {Object.values(historyStats).map(test => (
        <p>{test[0]} | {test[1]}</p>
      ))}
      </div>
    )
  }
}


export default Chart;

我怎样才能实现这样的输出:

test[0]} = 1/22/20 - 日期

test[1] = 0 - 值

帮助表示赞赏 谢谢

【问题讨论】:

  • 您想要的预期结果是什么?因为你只在你的回报中显示&lt;p&gt; germany | Object &lt;p&gt; 之类的东西,因为test[1]是来自键timeline的对象
  • 我想输出,像这样:{test[0]} = 1/22/20 | {test[1] = 0
  • @BloodOverdrive 编辑帖子并在那里添加输出,从这里看有点不可读

标签: javascript json multidimensional-array


【解决方案1】:

好吧,问题是您没有正确访问您的项目,并且您将historyStats 定义为一个数组,但响应是一个对象,理想情况下,最好的做法是将数据转换为您想要的。让我们去做吧。

还可以使用Object Entries 来获取我们的[key, value]

import React, { Component } from 'react'

class Chart extends Component {
  state = {
    historyStats: [],
    // error: true
  };

  componentDidMount() {
    fetch('https://corona.lmao.ninja/v2/historical/germany')
    .then(res => res.json())
    .then((data) => {
      // basically, lets get what it is important, the CASES.
      const { timeline } = data;
      const { cases } = timeline;

      // now cases is an object, lets transform it into an array of 2 entries, the key and value.
      const newHistoryStats = Object.entries(cases).map(pair=> pair);
      this.setState({ historyStats: newHistoryStats })

    })
    .catch(console.log)
  }

  render() {
    const {historyStats} = this.state;
    console.log(Object.values(historyStats));

    return (
      <div>
        {
          historyStats.map(test => ( <p>{test[0]} | {test[1]}</p> ))
        }
      </div>
    )
  }
}


export default Chart;

基本上我们正在创建这样的条目:

fetch('https://corona.lmao.ninja/v2/historical/germany')
  .then(res => res.json())
  .then((data) => {
    // basically, lets get what it is important, the CASES.
    const { timeline } = data;
    const { cases } = timeline;

    // now cases is an object, lets transform it into an array of 2 entries, the key and value.
    const newHistoryStats = Object.entries(cases).map(pair => pair);
    
    // this will be similar to the render
    newHistoryStats.map(test => console.log(test[0], "|", test[1]))
  })

【讨论】:

  • 感谢您的回答,它在 console.log 中完美运行,但是当我尝试传入渲染并返回时,它会抛出 Uncaught TypeError: Cannot read property 'map' of未定义。我不确定我做错了什么:render() { const { newHistoryStats } = this.state; return ( &lt;div&gt; {newHistoryStats.map(test=&gt;&lt;li&gt;{test[0]} | {test[1]}&lt;/li&gt;)} &lt;/div&gt; ); }
  • 不,因为你必须这样做:render() { const { historyStats } = this.state; return ( &lt;div&gt;{historyStats.map(test=&gt;&lt;li&gt;{test[0]} | {test[1]}&lt;/li&gt;)} &lt;/div&gt; ); }
  • newHistoryStats 只是一个示例名称 :) 您的状态变量将始终为 historyStats
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2012-11-12
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多