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 }
Run Code Online (Sandbox Code Playgroud)
我该如何实现?因为我使用它的方式,所以名称始终是“ slide”,但是我希望它以0作为名称。
您可以执行以下操作:
const object = { [slide]: result };
Run Code Online (Sandbox Code Playgroud)
或另一种方法是:
const object = {};
object[slide] = result;
Run Code Online (Sandbox Code Playgroud)