2
0

More than 3 years have passed since last update.

Node.js 16.6.0からArray.at()が使えるようになった

Last updated at Posted at 2021-08-06

Changelog

Reference

.at()とは

  • -1を渡すと末尾の要素を、-2を渡すと末尾から2番目の要素を返す、ブラケット[]の拡張。
  • Array[Array.length - 1]Array.at(-1)のように書ける。簡単。嬉しい。
  • 去年の11月からTC39 ProcessのStage 3に格上げされ、Chrome 92 および Firefox 90から利用可能となっている。
    https://github.com/tc39/proposal-relative-indexing-method

その他の提案

もっとシンプルに自由に配列にアクセスしたい、という思いから提案された仕様は他にもある。

ブラケットのスライス表記

  • 元からあるslice()メソッドが分かりにくすぎる!という提案。
  • PythonやGolang等にある[1:3] [-1:]のような書き方を追加するというもの。
  • 良さそうだけどドラフトになる気配なし…

.lastItem .lastIndex

  • その名の通り、最後の要素を指すlastItemと、最後のインデックスを指すlastIndexを追加する というもの。
  • .at()又はスライス表記のどちらかがStage 3に進めば、提案は破棄という予定だったので消滅。
2
0
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
2
0