javascript - Navigating to previous item inside map in React - Stack Overflow

Is that possible navigate to previous item, inside a map using React, I would like to parer the actual

Is that possible navigate to previous item, inside a map using React, I would like to parer the actual iteration with before, if the before have different group, per example, I will show the new one.

I would like to do something like that:

{navigation.map((item, i) => {
  return(
    <li key={i}>
      {i > 0 && item[i].groupName != item[i - 1].groupName && <div>Another group: {item[i].groupName}</div>}
    </li>
  )
})}

Is that possible navigate to previous item, inside a map using React, I would like to parer the actual iteration with before, if the before have different group, per example, I will show the new one.

I would like to do something like that:

{navigation.map((item, i) => {
  return(
    <li key={i}>
      {i > 0 && item[i].groupName != item[i - 1].groupName && <div>Another group: {item[i].groupName}</div>}
    </li>
  )
})}
Share Improve this question edited Jan 31, 2018 at 23:13 asked Jan 31, 2018 at 23:06 user8321027user8321027 6
  • What is before? – zerkms Commented Jan 31, 2018 at 23:07
  • the item before the actual in the loop, sorry for my english x( – user8321027 Commented Jan 31, 2018 at 23:10
  • Then what does "navigate to the previous item" mean? – zerkms Commented Jan 31, 2018 at 23:10
  • I tried show a code sample – user8321027 Commented Jan 31, 2018 at 23:10
  • I would like to acces the previous item in the array, example items[1,2,3,4,5], my iteration if was in the third I would like to pare with the second, like my sample – user8321027 Commented Jan 31, 2018 at 23:13
 |  Show 1 more ment

1 Answer 1

Reset to default 7

Yes, but it is not React-specific. It is JavaScript Array.map.

{navigation.map((item, i, arr) => {
  const previousItem = arr[i - 1];
  return(
    <li key={i}>
      {i > 0 && item[i].groupName != item[i - 1].groupName && <div>Another group: {item[i].groupName}</div>}
    </li>
  )
})}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742380564a4433030.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信