[Solved] How to add sequence to collections.map?

#1

Hi all,
I use the collections.map function then I would like to add the sequence 1,2,3,4,… to the result.
How can I make this work?

Below is my code (sequence_that_match_query is the variable that I want to put the sequence in it) :


<tbody>
      {posts.map(({ _id, salution, firstname, lastname, province, country, status }) => (
       <tr key={_id}>
		<td>{sequence_that_match_query}</td>
		<td>{salution}</td>
        <td>{firstname}</td>
        <td>{lastname}</td>
        <td>{province}</td>
        <td>{country}</td>
        <td>{status}</td>
	</tr>
	))}
</tbody>
#2

I use the variable to count the number of each document.

count = 0;

<tbody>
      {posts.map(({ _id, salution, firstname, lastname, province, country, status }) => (
       <tr key={_id}>
		<td>{count += 1}</td>
		<td>{salution}</td>
        <td>{firstname}</td>
        <td>{lastname}</td>
        <td>{province}</td>
        <td>{country}</td>
        <td>{status}</td>
	</tr>
	))}
</tbody>

I could get the result which works fine, but every time I click the navigation, the count += 1 add more counting,
until I refresh the count value reset to 0.

How can I prevent this and reset it everytime clicking the page navigation?

#3

the map function has another parameter as index

posts.map((item, index) => {
 consol.log(index+1)
 //because index starts with zero
})
1 Like
#4

iamrommel, Thank you very much. The code is below !!

    {posts.map(({ _id, regsId, salution, firstname, lastname, sex, age, province, country, status, username }, index) => (
       <tr key={_id}>
		<td>{index +1}</td>