Tom
Tom

Reputation: 4033

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.

Upvotes: 1

Views: 31

Answers (1)

Abdul Ahmad
Abdul Ahmad

Reputation: 10021

you can do the following:

const object = { [slide]: result };

or another way to do this is:

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

Upvotes: 5

Related Questions