Limit items in a .map loop

后端 未结 1 1266
礼貌的吻别
礼貌的吻别 2020-12-02 18:09

I would like to ask how can I limit my .map loop for example to a 5 items only because currently when I access an api it returns 20 items. but I want to display

相关标签:
1条回答
  • 2020-12-02 18:54

    You could use Array#slice and take only the elements you need.

    var film = this.props.data.slice(0, 5).map((item) => {
            return <FilmItem key={item.id} film={item} />
        });
    
    return film;
    

    If you do not neet the original array anymore, you could mutate the array with seting length to 5 and iterate then.

    0 讨论(0)
提交回复
热议问题