简体   繁体   中英

How do I put a variable as a "correct" existing element in object?

Let's say I have got an object

const myObject = {
    variable1: true,
    variable2: false
}

and just before, I've just got my variable from another code, another component and I know this will be equal to variable1 or variable2 named freshVariable How now do I use this variable to return correct value from myObject?

I can not use

console.log(myObject.freshVariable)

because freshVariable is not an variable existing in my object. I can not use

console.log(myObject.valueOf(freshVariable))

neither.

What can I do with it?

通过这种方式使用动态变量或键:

myObject[freshVariable] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM