narutaro / note

0 stars 0 forks source link

JavaScript #7

Open narutaro opened 1 year ago

narutaro commented 1 year ago

JavaScriptでObjectのKeyに変数展開する

Keyに'hoge'のように変数展開したいときは,[newKey]のように[]で変数名を囲います。

const newKey = 'hoge'
const obj = {
  [newKey]: 'test'
};
console.log(obj) // { hoge: 'test' }

ちなみに,この書き方はES2015以降で使える方法です。