[英]How do I put a variable as a "correct" existing element in object?
假設我有一個對象
const myObject = {
variable1: true,
variable2: false
}
就在之前,我剛剛從另一個代碼、另一個組件中獲取了我的變量,我知道這將等於名為freshVariable
variable1
或variable2
現在如何使用此變量從 myObject 返回正確的值?
我不能用
console.log(myObject.freshVariable)
因為 freshVariable 不是我的對象中存在的變量。 我不能用
console.log(myObject.valueOf(freshVariable))
兩者都不。
我可以用它做什么?
通過這種方式使用動態變量或鍵:
myObject[freshVariable]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.