Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Created June 27, 2020 09:55
Show Gist options
  • Save tonkotsuboy/efca15d750dd129b2c6b8e9e6b6a66b0 to your computer and use it in GitHub Desktop.
Save tonkotsuboy/efca15d750dd129b2c6b8e9e6b6a66b0 to your computer and use it in GitHub Desktop.
配列の最初の要素、長さ、最終要素をワンライナーで取得する
// 配列の最初の要素、長さ、最終要素をワンライナーで取得する
// 参考: https://twitter.com/argyleink/status/1139415654132436993
// 対象の配列
const myArray = ["😸", "🐶", "🐟", "🦆", "🐳", "🦉"];
// firstに最初の要素、lに長さ, lastに最終要素
const { 0: first, length: l, [l-1]: last} = myArray;
// 確認のため出力。結果は{first: "😸", l: 6, last: "🦉"}
console.log({first, l, last});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment