简体   繁体   中英

Assign name from property to object

I'm trying to create an object that takes its name from another property.

const slide = 0;
const result = 'Hello';
const object = { slide: result };

How I want the object to look like in the end:
{ 0: result }

How can I achieve this? Because the way I use it, the name will always be "slide", but I want it to take the 0 as a name.

you can do the following:

const object = { [slide]: result };

or another way to do this is:

const object = {};
object[slide] = result;

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