Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@ledsun

JavaScriptのオブジェクトのキーに配列が使える

オブジェクトのキーに配列を使う

var a = {};
a[['key']] = 1;
a[['key']]; //1が取れる

オブジェクトリテラル中でキーに配列を使うと構文エラー

var a = {['key'], 1};

種明かし

配列が文字列化されてキーになっています。

['key']を文字列化すると "key" になります。

String(['key']) // "key"が返る

実際はkeyプロパティが設定されています。

var a = {};
a[['key']] = 1;
a.key; //1が取れる

仕様確認

Working with Objects - JavaScript | MDN

オブジェクトプロパティの名前には、正しい JavaScript 文字列か、空文字列を含む、文字列に変換できるあらゆるものを使えます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
8
Help us understand the problem. What are the problem?