简体   繁体   English

使用相同的索引来映射React.js中的两个数组

[英]Using the same index to map two arrays in React.js

I want to display a table in React. 我想在React中显示一个表格。 The first column is all the courses taught by the prof, and the second column is at most three comments associated with that course. 第一列是教授教授的所有课程,第二列是与该课程相关的最多三个注释。 Data for all the courses are stored at 所有课程的数据都存储在

this.state.courses

All the comments are nested array mapping to the course order. 所有评论都是嵌套数组映射到课程顺序。 Access comments for the first course is 访问第一门课程的评论是

this.state.comments[0]

My code for now did not work, but this is my attempt 我的代码目前无法正常工作,但这是我的尝试

                    <Table striped bordered hover>
                        <thead>
                            <tr>
                                <th>{`Courses Taught By ${this.state.professorname}`}</th>
                                <th>{`Comments on Course`}</th>
                                <th>{`Comments on Prof`}</th>
                            </tr>
                        </thead>

                        <tbody>
                            <tr>
                                {this.state.courses.map((course, i) => (
                                    <td key={i}>
                                        <Link to={`/${this.state.id}/${course._id}`}>{course.name}</Link>
                                    </td>
                                ))}
                                {this.state.comments[i].map((comment, j) => {
                                    <td key={j}>
                                        <p>{this.state.comments[i][j]}</p>
                                    </td>
                                })}
                            </tr> 
                        </tbody>
                    </Table>

I could really the help for this one. 我真的可以为这个提供帮助。 Thanks! 谢谢!

The second loop is out of the scope of the first one - it does not have access to i . 第二个循环不在第一个循环的范围之内-它无法访问i

You should move the second map inside the first one, to be able to access it. 您应该将第二张map移到第一张map内,以便能够访问它。

<tr>
    {this.state.courses.map((course, i) => (
       <>
          <td key={course._id}>
             <Link to={`/${this.state.id}/${course._id}`}>{course.name}</Link>
          </td>
          {this.state.comments[i].map((comment, j) => (
             <td key={j}>
                <p>{this.state.comments[i][j]}</p>
             </td>
          ))}
      </>
    ))}                                   
</tr> 

Two problem I can see in your code. 我可以在您的代码中看到两个问题。

First i from first loop is not available in second loop(out of scope). 第一个循环中的第一个i在第二个循环中不可用(超出范围)。

Second you are not returning anything in second map functions. 其次,您在第二个地图函数中未返回任何内容。

<tr>
{this.state.courses.map((course, i) => (
   <>
      <td key={course._id}>
         <Link to={`/${this.state.id}/${course._id}`}>{course.name}</Link>
      </td>
      {this.state.comments[i].map((comment, j) => {
  return (
         <td key={j}>
            <p>{this.state.comments[i][j]}</p>
         </td>
      )
   })}
  </>
))}                                   
</tr> 

在中使用.map<div> 标签-react.js</div><div id="text_translate"><p> 我真的很感激任何帮助。 所以该项目是一个反应项目。</p><p> 这是我获取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的内容:获取请求</a></p><p>这是我从服务器响应的内容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我试了一下 Postman,看看我收到的是什么类型的 json。 这是它返回的内容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 测试</a></p><p>我遇到的问题是我无法 map 收到的数据。 一旦输入链接,我只想在首页上以一种很好的方式显示接收到的数据。</p><p> 是否可以将 map 放在 div 标签内? 或者还有另一种方法吗?</p><p> <em><strong>请注意该项目是作为功能组件编写的</strong></em></p></div> - Using .map in <div> tag - react.js

暂无
暂无

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

相关问题 在中使用.map<div> 标签-react.js</div><div id="text_translate"><p> 我真的很感激任何帮助。 所以该项目是一个反应项目。</p><p> 这是我获取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的内容:获取请求</a></p><p>这是我从服务器响应的内容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我试了一下 Postman,看看我收到的是什么类型的 json。 这是它返回的内容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 测试</a></p><p>我遇到的问题是我无法 map 收到的数据。 一旦输入链接,我只想在首页上以一种很好的方式显示接收到的数据。</p><p> 是否可以将 map 放在 div 标签内? 或者还有另一种方法吗?</p><p> <em><strong>请注意该项目是作为功能组件编写的</strong></em></p></div> - Using .map in <div> tag - react.js 在 react.js 中使用 Array.map - Using Array.map in react.js 使用 react.js 从-到映射 - Map from-to using react.js 类型错误:map 不是 function(使用 React.js) - TypeError: map is not a function (Using React.js) React.js map()不是函数吗? - React.js map() is not a function? React.js和Mapbox:使用react.js时,mapbox map无法正确呈现 - React.js and Mapbox: mapbox map not rendering properly when using react.js 地图值react.js - Map value react.js React.js不会区分同一类的两个组件 - React.js does not differentiate between two components of same class React.js与'两个孩子用同一把钥匙'混淆 - React.js confuse with 'two children with the same key' React.js:同一父母的两个孩子之间的交流? - React.js: Communication between two children of the same parent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM