const key = 'apple'
const obj = {
apple: 'red',
banana: 'yellow'
}
const appleColor = obj[key]
console.log(appleColor)//red
【JavaScript】オブジェクトのキーに変数を指定して取得

const key = 'apple'
const obj = {
apple: 'red',
banana: 'yellow'
}
const appleColor = obj[key]
console.log(appleColor)//red
コメント