首页 > 解决方案 > 如何根据数组中的最后一个索引隐藏元素?

问题描述

所以我有一个对象数组,我们称之为questions

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

所以这基本上就是我所需要的;ElementToShowIfItIsNotTheLastIndexOfTheQuestionsArray当元素到达questions数组的最后一个索引时隐藏元素。

标签: javascriptarraysreactjsecmascript-6

解决方案


您可以slice在调用之前删除数组的最后一个元素map

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

推荐阅读