0

I am currently stuck in a personal project and I can't put the right words in google to find the solution...

I have a map in my code from a collection in a database, this map shows a <Card /> with info on it like so:

.map((personnage) => (
    <Card
    key={personnage._id}
    id={personnage._id}
    name={personnage.name}
    job={personnage.job.toLowerCase()}
        firstBounty={personnage.bounties[0]?.toLocaleString()} /*Separate the first bounty with space, the ? is to verify if there is a value, else it won't work*/
    lastBounty={personnage.bounties
            .slice(-1)
            .toLocaleString()} // Separate the last Bounty with space
    firstGrade={personnage.grade[0]?.index}
    lastGrade={personnage.grade.slice(-1)?.index}
/>
))

the .json would look like

{
  grade: [{ index: 5, name: "something"}, { index: 12, name: "something else"}]
}

So I tried to replace slice(-1).index by slice().reverse().index but it doesn't work... And I can't find the logic behind it...

So I'm just trying to get the last grade of the grade array. And pass the index to the lastGrade prop.

Thanks for your help kind strangers!

RubenSmn
  • 4,091
  • 2
  • 5
  • 24

1 Answers1

0

You can do this by using the length of the array.

You need to substract 1 since the array starts with index 0

<Card
  ...
  lastGrade={personnage.grade[personnage.grade.length - 1]?.index}
/>
RubenSmn
  • 4,091
  • 2
  • 5
  • 24