我有一个变量标签
。 options[0]
中可以缺少属性 label
。我在这里使用可选的链接运算符。有没有更好的方法来解构标签属性?
代码如下所示:
const label = screenItems.vendor.options[0]?.label
最佳答案
You are not destructuring here. and that code should work fine...
但是如果你真的想破坏你的解决方案。使用下面的代码片段...
// assuming you have `screenItems.vendor.options` defined already
const options = screenItems.vendor.options
const { label } = options.length > 0 ? options[0] : {}
if (label) {
// all the code that requires the label
}
在这种情况下,代码也多得多。
关于javascript - 这是解构对象属性的更好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443804/