为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 ReactJS 的 map 函数中使用 For Loop

如何在 ReactJS 的 map 函数中使用 For Loop

慕的地8271018 2023-03-10 13:58:36
for 循环在 map 函数中不起作用我在 map 函数中创建 map 函数添加 if 条件并希望在每次迭代中更改 ID 值。为此,我尝试了 for 循环,但这还没有用。如何在 map 函数中使用 For Loop?{items.map((d) => (        <Accordion key={d.ID}        title={            <div>              <tr className="btn-heading">                <td>{d.ID}</td>                <td>{d.Mail}</td>                <td>{d.Name}</td>                <td>{d.PhoneNo}</td>                <td>{d.City}</td>                <td>{d.Date}</td>                <td>{d.Time}</td>              </tr>            </div>          }          content={            <div>              <p className="header">                  <span className="header-content">Shipping Address:</span>                  292 Naqshband Colony. Near rabbania Mosque. Multan              </p>              <Table size="sm">                <thead>                  <tr>                    <th>#</th>                    <th>Article No</th>                    <th>Product Name</th>                    <th>Quantity</th>                    <th>Price</th>                    <th>Total Amount</th>                  </tr>                </thead>                <tbody>                  for (var i = 0; i < d.ID; i++) {  <--- for loop does'nt working                    {products.map((c) =>                      c.ID === 1 ? (                                   <tr key={c.ID}>                            <td>{c.ArticleNo}</td>                            <td>{c.ProductName}</td>                            <td>{c.Quantity}</td>                            <td>{c.Price}</td>                            <td>{c.TotalAmount}</td>                          </tr>                      ) : null                                                    )}                     }                  </tbody>              </Table>             </div>          }        />      ))}这是我的代码箱: https://codesandbox.io/s/hungry-bardeen-8m2gh ?file=/src/App.js
查看完整描述

1 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

...


function App() {

  const [items, setItems] = useState([]);

  const [products, setProducts] = useState([]);


  const renderProducts = (d, products)=> {

    const result = []

    for (var i = 0; i < d.ID; i++) {  

      let product = products.map((c) =>

        c.ID === 1 ? (         

            <tr key={c.ID}>

              <td>{c.ArticleNo}</td>

              <td>{c.ProductName}</td>

              <td>{c.Quantity}</td>

              <td>{c.Price}</td>

              <td>{c.TotalAmount}</td>

            </tr>

        ) : null                              

        )

        result.push(product)

      }

    return result

  }


  ... 


  return (

    <div className="container-fluid">

      <section className="heading">

        <h4>Products Details</h4>

        <input

          type="file"

          className="input-field"

          onChange={(e) => {

            const file = e.target.files[0];

            readExcel(file);

          }}

        />

      </section>

      {items.map((d) => (

        <Accordion

          title={

            <div>

              <tr key={d.ID} className="btn-heading">

                <td>{d.ID}</td>

                <td>{d.Mail}</td>

                <td>{d.Name}</td>

                <td>{d.PhoneNo}</td>

                <td>{d.City}</td>

                <td>{d.Date}</td>

                <td>{d.Time}</td>

              </tr>

            </div>

          }

          content={

            <div>

              <p className="header">

                <span className="header-content">Shipping Address:</span>

                292 Naqshband Colony. Near rabbania Mosque. Multan

              </p>

              <Table size="sm">

                <thead>

                  <tr>

                    <th>#</th>

                    <th>Article No</th>

                    <th>Product Name</th>

                    <th>Quantity</th>

                    <th>Price</th>

                    <th>Total Amount</th>

                  </tr>

                </thead>

                <tbody>

                  {renderProducts(d, products)}

                </tbody>

              </Table>

            </div>

          }

        />

      ))}

    </div>

  );

}

export default App;


查看完整回答
反对 回复 2023-03-10
  • 1 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号