亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在React JSX中循環遍歷數組中的某些項目

如何在React JSX中循環遍歷數組中的某些項目

慕勒3428872 2022-01-07 20:44:36
我的問題是關于如何部分迭代 React JSX 中的數組。我不想調用 .map 并遍歷 profile.categories 中的所有項目,而只想顯示數組中的前五個項目。我目前有以下代碼:<div className="categories">   {profile.categories.map(category => (       <div         className="profile-categories"         style={{ float: "left" }}       >         {category}       </div>     ))} </div>
查看完整描述

2 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

直接在 profile.categories 上使用 slice,如下所示:


<div className="categories">

{profile.categories.slice(0, 5).map(category => (

   <div

     className="profile-categories"

     style={{ float: "left" }}

   >

     {category}

   </div>

 ))}

 </div>


查看完整回答
反對 回復 2022-01-07
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

只需將切片與地圖一起使用:


profile.categories.slice(0, 5).map(...)

您還可以添加方法來獲取組件中的一些類別計數:


getFirst(count) {

  return profile.categories.slice(0, count);

}


// and then in render:

this.getFirst(5).map(...)


查看完整回答
反對 回復 2022-01-07
  • 2 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號