a have array like this
array = [{name : john , age:20},{name : jack, age:24},{name : rose, age:26},{name :
david, age:40}]
the array.length is 4 and i wanna mapped in react like this
array.map((list)=>(
<h1>{list.name}</h1>
<h2>{list.age}</h2>
))
but i wanna mapped to first two object in array like array.lentgh = 2
and gets output like this
<h1>john</h1>
<h2>20</h2>
<h1>jack</h1>
<h2>24</h2>
because i wanna mapped two other object some where else
how to do that
2
Answers
Use Array.slice:
Note 1: your name values in your objects must be in quotes.
Note 2: inside your mapped data your markup needs to be wrapped in either a containing element or a React fragment.
Note 3: ideally you want to add an id to your objects that you can use as a key on the wrapping element (rather than using the
map
index).I see, this is more like a programming thing, there’s lots of ways. Other than the
slice
, which could be the best safe way. If you just start programming, you can try the following as well.As I said, this is programming, do whatever to make it work then. For example, just copy paste twice that’ll solve your problem as well. Or do a loop and skip when index is bigger than two.