4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【React】mapのループ内で最後の要素にだけ処理を行う

Posted at

概要

mapで繰り返ししている要素の最後の要素にだけスタイルを変えたいみたいなのがありあまり調べても出てこなかったのでメモとして残しておきます。

実装

const list = [
  {name: "john1"},
  {name: "john2"},
  {name: "john3"},
]

return (
  <div>
    {list.map((l, i) => (
      <div style={(list.length - 1 === i) ? "最後の要素に付与するクラス名" : "最後以外に付与するクラス名"}>{l.name}</div>
    )}
  </div>
)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?