简体   繁体   English

无法读取嵌套数组与 MAP 反应

[英]Can't read nested Array with MAP react

i'm in trouble with my nested array.我的嵌套数组有问题。 i have an object:我有一个 object:

{ name: 'house', 
  url: 'www.mockhouse.com', 
  thumb: myImage, 
  describe: 'another project', 
  tech: ['react', 'fetch', 'SASS', 'HTML'] },

when i try to map tech array (that is a nested array) like:当我尝试 map 技术阵列(即嵌套阵列)时,例如:

   <GalleryWrapper>
    {props.projectArray.map((projects) => {
      return (
        <ThumbWrapper key={uuidv4()}>
          <ThumbImg src={projects.thumb} alt="" />
          <ThumbTitle> {projects.name}</ThumbTitle>
          <ThumbDescrib>{projects.describe}</ThumbDescrib>

          {projects.tech.map((techno) => {
            return <ThumbTech>{techno}</ThumbTech>; // here is the problem //
          })}

        </ThumbWrapper>
      );
    })}
  </GalleryWrapper>

it dosen't work and i have an error: Project.jsx:67 Uncaught TypeError: Cannot read properties of undefined (reading 'map')它不起作用,我有一个错误:Project.jsx:67 Uncaught TypeError: Cannot read properties of undefined (reading 'map')

i dont know why it dosen't work any idea?我不知道为什么它不起作用任何想法?

thanks:)谢谢:)

Maybe in some objects of the array the tech property does not exist.也许在数组的某些对象中, tech属性不存在。 Please make sure this is a defined array and not an optional property in the object.请确保这是一个已定义的数组,而不是 object 中的可选属性。 Just in case, if it is an optional property or it is undefined for some objects in the array then you can use ?以防万一,如果它是可选属性,或者对于数组中的某些对象未定义,那么您可以使用? on you loop like this:在你这样循环:

   <GalleryWrapper>
    {props.projectArray.map((projects) => {
      return (
        <ThumbWrapper key={uuidv4()}>
          <ThumbImg src={projects.thumb} alt="" />
          <ThumbTitle> {projects.name}</ThumbTitle>
          <ThumbDescrib>{projects.describe}</ThumbDescrib>

          {projects.tech?.map((techno) => {
            return <ThumbTech>{techno}</ThumbTech>; // here is the problem //
          })}

        </ThumbWrapper>
      );
    })}
  </GalleryWrapper>

? will allow you to evaluate the code only if the statement before ?仅当声明之前才允许您评估代码? evalues to a defined value. evalues 到一个定义的值。

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

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