Javascript Map Array Last Item

后端 未结 7 677
滥情空心
滥情空心 2020-12-13 16:57

I have this:

map = ranks.map((row, r) => (
  row.map((rank, i) => {
    return [element(r, i, state, rank, toggled, onClick)];
  })
));
7条回答
  •  旧巷少年郎
    2020-12-13 17:51

    A slight improvement on the accepted answer:

    const lastIndex = row.length - 1;
    row.map((rank, i) => {
      if (i === lastIndex) {
        // last one
      } else {
        // not last one
      }
    })
    

    This removes the arithmetic from inside the loop.

提交回复
热议问题