javascript - 空检查对象不起作用
问题描述
我正在尝试检查空值,即使我知道这些值为空,循环仍然不会中断。为任何帮助而欢呼
constructor(){
super();
this.state = {
warningMsg: 'No Warnings'
}
this.detail = {
name: null,
phone: null,
email: null,
location: null,
extraDetail: 'Default'
}
}
handleSubmit(){
const {DetailStore} = this.props;
for (let value in this.detail) {
console.log(value)
if (value === null) {
console.log('null found'); // i should see this in console but i don't
this.setState({warningMsg:'Check Input'});
break;
}
}
DetailStore.entDetail(this.detail);
console.log(DetailStore.getDetail,'Submitted');
}
解决方案
for..in
loops iterate over the property names of the object, not the value of the property. If you want to iterate over object values, better to use Object.values
instead:
if (Object.values(this.detail).some(value => value === null)) {
console.log('null found');
this.setState({warningMsg:'Check Input'});
}
推荐阅读
- kubernetes - Kunernetes/kustomize 服务端点异常行为
- ios - 无法以编程方式移动 UILabel 位置
- sql - 将表与中间映射表匹配(具有相似字符串的模糊连接)
- c# - 文本框中的空格分隔符
- python - Beautifulsoup 只产生结果
- docker - Docker:打开 tcp 套接字失败
- java - 加入Hibernate是否需要明确指出关联部分?
- c# - 来自 Azure Function 应用的可靠/幂等邮件
- drupal-8 - Drupal 8用户注册rest api逐步创建
- reactjs - 如何设置 Visual Studio Code(VSC) 或 Sublime Text3 来运行 ReactJS