简体   繁体   English

同时映射2个arrays

[英]Mapping over 2 arrays at the same time

SO, I have 2 arrays of objects: meeting and room and a Card component with some fields to be completed by them.所以,我有 2 个 arrays 对象:会议和房间以及一个卡片组件,其中一些字段由它们完成。 I tried to map over the arrays in order to access the elements from both of them at the same time but NOW it returns the Card component six times(the number of the elements in the array of Room)我尝试在 arrays 上使用 map 以便同时访问它们中的元素,但现在它返回 Card 组件六次(Room 数组中的元素数)


 <div className="topCards">
            {room.map((dataObj) => {
              return (
                <div>
                  {meeting.map((meetObject) => {
                    return (
                      <div className="item">
                        <Card3
                          className="card3"
                          teamName={meetObject.name}
                          roomName={dataObj.name}
                          time={dataObj.time}
                          data={dataObj.data}
                          capacity={dataObj.capacity}
                        />
                      </div>
                    );
                  })}
                </div>
              );
            })}
          </div>

So, my question is how can I return the Card element with elements from both cards所以,我的问题是如何返回带有两张卡片中的元素的 Card 元素

What let you think that it would work like that?是什么让你认为它会那样工作?

Just use the index parameter of Array.prototype.map只需使用Array.prototype.mapindex参数

<div className="topCards">
  <div>
    {meeting.map((meetObject, i) => {
      return (
        <div className="item">
          <Card3
            className="card3"
            teamName={meetObject.name}
            roomName={dataObj[i].name}
            time={dataObj[i].time}
            data={dataObj[i].data}
            capacity={dataObj[i].capacity}
          />
        </div>
      );
    })}
  </div>
</div>

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

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