LoginSignup
5
3

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

Last updated at Posted at 2023-05-22

概要

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

方法

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

    let keyStr = 'keyName';
    
    let infos = {
        [keyStr] : 'value',
    };
    
    console.log(infos);
    
5
3
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
5
3