0

What should i do to stop rendering all the items from array in map() and limit to a required number (let's say 4)?

1 Answer 1

5

you can use slice method to limit the number of elements to n in the map:

let renderedData = this.state.elements.slice(0,n).map((element)=>{
 return (<div key={element.id}>{element.txt}</div>)
}

if you want to map only 4 then use 4 in place of n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.