【问题标题】:Accessing table objects data from the JSON file从 json 文件访问表对象数据
【发布时间】:2022-11-25 15:42:08
【问题描述】:

display json data in the table

我在访问我的 json 数据时遇到问题,它位于对象的对象数组中如何做到这一点。我正在使用 map 函数来仅显示它嵌套的名称和头像。

json数据

[
  {
    "id": "1",
    "person": {
      "name": "Prerna Jha",
      "avatar": "profile.jpg"
    },
    "city": "Mumbai",
    "email": "prernajha@gmail.com",
    "joiningDate": "12/02/2018",
    "role": "UI Designer"
  },
]




{data.map((curElem) => {
          const { id, name, avatar, city, email, joiningDate, role } = curElem;
          return (
            <tbody key={id}>
              <tr>
                <td>{name}</td>
                <td>{avatar}</td>
                <td>{city}</td>
                <td>{email}</td>
                <td>{joiningDate}</td>
                <td>{role}</td>
              </tr>
            </tbody>
          );
        })}

我得到的数据很少,嵌套对象没有得到。

【问题讨论】:

    标签: javascript reactjs frontend


    【解决方案1】:

    需要在主 json 中首先访问 person 对象

       const { id, person, city, email, joiningDate, role } = curElem;
       const {name, avatar} = person
    

    【讨论】:

    • 是的,明白了。非常感谢
    • @Ashish 如果有帮助,请检查是否已回答
    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多