LoginSignup
3

posted at

js 変数に入った文字列をオブジェクトのキーに設定する

概要

  • 予め変数に格納した文字列をオブジェクトのキーとして設定する方法を簡単にまとめる。

方法

  • 下記のように、キー部分を[]で囲むことで変数が展開されて、変数の中身をキー名として設定する事ができる。

    let keyStr = 'keyName';
    
    let infos = {
        [keyStr] : 'value',
    };
    
    console.log(infos);
    

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
What you can do with signing up
3