在Reactjs中渲染每个数组项?

zpgglvta  于 2023-11-18  发布在  React
关注(0)|答案(1)|浏览(109)

试图从我的generateSeats函数中呈现每一项,但我现在只呈现一个座位。我知道我的第一个函数应该返回一个数组,但在屏幕上呈现数组中的每一项让我为难。

const generateSeats = () => {
    let numRow = 8;
    let numColumn = 3;
    let rowArray = [];
    let start = 1;
    let reachnine = false;

    for(let i=0;i<numRow;i++) {
        let columnArray = [];
        for(let j=0;j<numColumn;j++) {
            let seatObject = {
                number : start,
                taken : Boolean(Math.round(Math.random())),
                selected: false, 
            }
            columnArray.push(seatObject);
            start++;
        }
        if(i==3){
            numColumn += 2
        }
        if(numColumn <9 && !reachnine) {
            numColumn +=2;
        } else {
            reachnine=true;
            numColumn -= 2;
        }
        rowArray.push(columnArray)
    }
    return rowArray
}

const PurchaseTicket = () => {
  return (
    <div className='w-full h-full  overflow-auto scrollbar-hide'>
        <div className='w-full h-full flex flex-1 bg-black'>
            <div className='bg-white w-[12px] h-[12px] rounded-t-[10px]'>{generateSeats}       
            </div>
        </div>
    </div>
  )

字符串

w7t8yxp5

w7t8yxp51#

也许像这样的东西对你有用?我已经使用map函数来获取每一行,然后用同样的方法来获取每个座位:

return (
    <div className="w-full h-full  overflow-auto scrollbar-hide">
      <div className="w-full h-full flex flex-1 bg-black">
        {generateSeats().map((row) => {
          return row.map((seat) => {
            return (
              <div className="bg-white w-[12px] h-[12px] rounded-t-[10px]">
                {/*Here you can access every value by seat.<value>*/}
                {seat.number}
              </div>
            );
          });
        })}
      </div>
    </div>
  );

字符串

相关问题