简化三元运算符

问题描述 投票:0回答:1

我想使用三元运算符在notEmpty上设置是或否,但是我的VSCode声纳标记用逗号用蓝色下划线标记:简化此表达式

代码:const notEmpty = list.lenght > 0 ? true : false;

这实际上有效,但可能会更好。

javascript typescript ternary sonarlint-vs
1个回答
0
投票

尝试一下:

const notEmpty = list.length ? true : false;

无需将其与0进行比较,因为它为空时返回0,这是false,否则为true

© www.soinside.com 2019 - 2024. All rights reserved.