简体   繁体   English

访问嵌套 JSON 对象的信息以进行 React 组件渲染

[英]Accessing the information of a nested JSON object for React component rendering

I'm trying to access the all of the items in the JSON object below我正在尝试访问下面 JSON 对象中的所有项目

{
"Member1": {
    "name": "",
    "position": "",
    "photoPath": ""
},
"Member2": {
    "name": "",
    "position": "",

    "photoPath": ""
},
"Member3": {
    "name": "",
    "position": "",

    "photoPath": ""
},
"Member4": {
    "name": "",
    "position": "",

    "photoPath": ""
}

} }

How would I go about looping through each member for their attributes for use in another object using the map() function?我将如何使用map()函数遍历每个成员的属性以在另一个对象中使用?

This is what I have so far: I also just throw in variable into the JSX using brackets到目前为止,这就是我所拥有的:我也只是使用方括号将变量放入 JSX 中

  var List = JSONObj.map((Member) => {
return (
  <MemberComponent
  name={Member.name} 
  </MemberComponent>
  );
});

Edit: Object.values.map() did it!编辑: Object.values.map() 做到了! Thanks for your help谢谢你的帮助

对于对象,请使用 Object api。

Object.keys(obj) // returns the list of keys

Object.values(obj) // returns the list of values

Object.entries(obj) // returns the list of entries (a tuple of key and value)

Below should be able to generate the list.下面应该可以生成列表。

const list  = Object.keys(myObject).map((member) => 
<MemberComponent name={Member.name} </MemberComponent>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM