简体   繁体   English

循环遍历数据 object 并将 Header 映射到 React 中的值

[英]Looping through the data object and Mapping Header to values in React

I have a question on how to use this data to map the headers to it's corresponding values and put that on the UI我有一个关于如何将这些数据用于 map 的标题到它的相应值并将其放在 UI 上的问题

This is how the data is structured:这是数据的结构方式:

 { "data": { "details": [ { "address_line_1": "CO Cwtsatotravel", "address_line_2": "Not Available", "city_name": "Arlington", "state_name": "-", "country_name": "Japan", "postal_code": "22203", "phone_number": "7638527755", } ] } }

This is what I am trying to do in react这就是我想要做的反应

 const profile_info = data?.details; const profileHeaders = [ 'Address1', 'Address2' 'City', 'State', 'Postal Code', 'Country', 'Phone', ]; return ( <Grid id="top-card" className={classes.mainContainer} container style={{ marginBottom: '4px', }} > {/* <Grid item md={11} lg={11} id="item-card"> */} <Grid container item> <Typography variant="subtitle1"> {profile_info.agency_name} </Typography> </Grid> <Grid container style={{ backgroundColor: '#f9f9f9', }} > {profileHeaders.map((v) => ( <Grid item style={{ padding: '0px 4px', }} > <Typography className={classes.profileData} gutterBottom={true}> {v} </Typography> <Typography className={classes.profileData}> {' '} {profile_info[v]} </Typography> </Grid> ))} </Grid> </Grid> );

When I do this, it's getting me all blank values on the UI for the headers当我这样做时,它会让我在 UI 上为标题获取所有空白值

Please help, thank you !请帮忙,谢谢!

There is some crucial erros on your code:您的代码中有一些关键错误:

  1. const profile_info = data?.details;

Your details are stored on property data.data.details , not data.details So fix this, first:您的详细信息存储在属性data.data.details上,而不是data.details所以先解决这个问题:

const profile_info = data?.data.details;
  1. The items in ProfileHeaders are not mapped like properties in profile_info: you have a profile_info['address_line_1'] , but not profile_info['Address1'] , wich is what you are trying to do in your component. ProfileHeaders 中的项目不像 profile_info 中的属性那样映射:您有profile_info['address_line_1'] ,但没有profile_info['Address1'] ,这就是您要在组件中执行的操作。

To make this work the way you want, you should map title and property correctly.要按照您想要的方式进行这项工作,您应该正确地使用 map 标题和属性。

const profileHeaders = [
    {
        title: "Address1",
        property:  "address_line_1"
    },
    {
        title: "Address2",
        property:  "address_line_2"
    },
    // map all other properties like above.
]

then you can go for that:那么您可以为此使用 go :

   {profileHeaders.map((v) => (
      <Grid
        item
        style={{
          padding: '0px 4px',
        }}
      >
        <Typography className={classes.profileData} gutterBottom={true}>
          {v.title}
        </Typography>
        <Typography className={classes.profileData}>
          {' '}
          {profile_info[v.property]}
        </Typography>
      </Grid>
    ))}

I am not checking if profile_info is undefined, but you must do it in your component to avoid errors.我没有检查 profile_info 是否未定义,但您必须在组件中执行此操作以避免错误。

Encode your headers using the same [as in data] keys :使用相同的[as in data]编码您的标题:

const headers = {
    "address_line_1": "Address1",
    "address_line_2": "Address2",
    "city_name": "City",

later, you can list it稍后,您可以列出它

Object.entries(headers).forEach(([key, value]) => console.log(`${key}: ${value}`));

console.log(data) to see its structure and use const to 'alias' iterable (object with props or array) element: console.log(data)查看其结构并使用const为可迭代(带有道具或数组的对象)元素“别名”:

// choose the right data source - depends on what logged out
// console.log(data);
// if(data) console.log(data.details); //... step by step

// const profile_info = data?.details;
// const profile_info = data?.data.details;
const profile_info = data?.details[0]; // object in array here

render values from both headers and profile_infoheadersprofile_info渲染值

Object.entries(headers).forEach(([key, value]) => (
 <Grid
    key={key}
    item
    style={{
      padding: '0px 4px',
    }}
  >
    <Typography className={classes.profileData} gutterBottom={true}>
      {value}
    </Typography>
    <Typography className={classes.profileData}>
      {' ??? use css instead! '}
      {profile_info[key]}
    </Typography>
  </Grid>

or you can do the same using .map (you can use index if required)或者您可以使用.map执行相同操作(如果需要,您可以使用index

  Object.keys(headers).map((key, idx) => (
    <Element key={key}>
       <Name title={headers[key]} />
       <Value data={profile_info[key]} />

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

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