const key = 'apple'
const obj = {
apple: 'red',
banana: 'yellow'
}
const appleColor = obj[key]
console.log(appleColor)//red
【JavaScript】オブジェクトのキーに変数を指定して取得
![デベロッパーツール](https://kamiya-note.com/wp-content/uploads/2021/09/undraw_proud_coder_7ain-1.png)
const key = 'apple'
const obj = {
apple: 'red',
banana: 'yellow'
}
const appleColor = obj[key]
console.log(appleColor)//red
コメント