{object}[index] 的语法是什么?
What is the syntax {object}[index] called?
在React中,我经常使用这种语法进行条件渲染:
const exampleState = 1;
const retval = {
0: "Value for state 0",
1: "Value for state 1",
2: "Value for state 2",
}[exampleState]
// this returns "Value for state 1"
这个语法叫什么?
编辑:添加 const retval
使其成为真正有效的代码
{ }
块定义了一个对象。 []
部分是 property accessor.
的括号符号
{
0: "Value for state 0",
1: "Value for state 1",
2: "Value for state 2",
}
是一个键:值对对象。
因此您的代码 returns 来自此对象的键 0
的值。
在React中,我经常使用这种语法进行条件渲染:
const exampleState = 1;
const retval = {
0: "Value for state 0",
1: "Value for state 1",
2: "Value for state 2",
}[exampleState]
// this returns "Value for state 1"
这个语法叫什么?
编辑:添加 const retval
使其成为真正有效的代码
{ }
块定义了一个对象。 []
部分是 property accessor.
{
0: "Value for state 0",
1: "Value for state 1",
2: "Value for state 2",
}
是一个键:值对对象。
因此您的代码 returns 来自此对象的键 0
的值。