How to map a list to a certain number of data

Viewed 53

Suppose i have a list having 10 datas. And inside return i want to limit to show only upto 5 data. How to acheive this.

{this.state.PackageCategoryData.map((data) => (
  <div>{data.name}</div>
))}
2 Answers

You can slice the array

{this.state.PackageCategoryData.slice(0,5).map((data) => (
  <div>{data.name}</div>
))}

You can use filter to show only 5 items from the array.

{this.state.PackageCategoryData.filter((data, index) => (
  if(index < 5) return <div>{data.name}</div>
))}
Related