javascript - 如果条件与三元运算符不返回真或假
问题描述
我正在比较 Nodejs 中的 2 个对象属性
if(alteredItem.main != (result.main)?result.main:"NULL"){
我在 VSCODE 中使用调试模式,所以当我检查时:
alteredItem.main = "232"
result.main = "232"
因此,我期望上述条件的结果是,False
但我的调试控制台显示该值为:
"NULL"
如果我删除三元运算符并将条件更改为:
if(alteredItem.main != result.main){
这确实返回false
。
为什么"NULL"
我使用三元运算符时它会返回?
解决方案
您正在混合使用if
语句和三元表达式。你应该做一个/或,但不能同时做:
选项1:
if(alteredItem.main != result.main){
return false;
} else {
return 'NULL'
}
选项 2:
// It's unclear from your question what you're expecting
// the return value to be, this assumes you want to
// return either `false` or `'NULL'`
return alteredItem.main != result.main ? false : 'NULL';
推荐阅读
- c++ - 是否保证不会发生类似函数的宏参数名称替换?
- python - 搁置 - 无法删除条目
- events - Tkinter:我可以绑定参数吗?
- asp.net - 双击动态生成的 web 控件触发
- composer-php - 我无法在我的网络主机中全局安装作曲家,如何使用“作曲家”命令?
- teradata - 使用 Teradata SQL 助手从文本文件中插入日期的正确格式是什么
- angular - 业力:如何调试 RangeError:超出最大调用堆栈大小?
- swift - Swift:线程安全的单例,为什么我们使用同步进行读取?
- r - 使用 roll = "nearest" 限制合并范围
- c# - LINQ 从 IQueryable 获取平均值