1
1

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 5 years have passed since last update.

JavaScriptでオブジェクト(Object)リテラルのキーに色々指定できる

Posted at

ES2015以降から柔軟にObjectを作成できるようになったみたい

オブジェクト初期化子 - JavaScript | MDN

const aa = () => 'AA'
const ii = 'II'

const o = {
  a: 'A',           // 通常のやつ
  [aa()]: 'ああ',   // メソッド呼び出しの返り値をキーにする
  [ii]: 'いい',     // 変数の値をキーにする
  ['UU']: 'うう',   // 文字列のリテラルをそのままキーにする
}

console.log(o)

> Object {
>   a: "A",
>   AA: "ああ",
>   II: "いい",
>   UU: "うう"
> }

こんな感じで色々キーにできるみたいです。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?