How to hide an element based on the last index in an array?

Viewed 145

So I have an array of objects, let's call it questions

{questions.length !== lastIndexOfTheQuestionsArray && (
  <ElementToShowIfItIsNotTheLastIndexOfTheQuestionsArray />
)}

So that is basically what I need; to hide the ElementToShowIfItIsNotTheLastIndexOfTheQuestionsArray element when it reaches the last index of the questions array.

1 Answers

You can slice the array to remove the last element before calling map.

array.slice(0, -1).map(/* ... */)
Related