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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.