Javascript 空检查对象不工作
我试图检查null值,即使我知道值为null,循环仍然不会中断。为任何帮助干杯Javascript 空检查对象不工作,javascript,ecmascript-6,Javascript,Ecmascript 6,我试图检查null值,即使我知道值为null,循环仍然不会中断。为任何帮助干杯 constructor(){ super(); this.state = { warningMsg: 'No Warnings' } this.detail = { name: null, phone: null, email: null, location: null, extraDeta
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
循环迭代对象的属性名称,而不是属性值。如果要迭代对象值,最好使用object.values
:
if (Object.values(this.detail).some(value => value === null)) {
console.log('null found');
this.setState({warningMsg:'Check Input'});
}
for..in
循环迭代对象的属性名称,而不是属性值。如果要迭代对象值,最好使用object.values
:
if (Object.values(this.detail).some(value => value === null)) {
console.log('null found');
this.setState({warningMsg:'Check Input'});
}
for循环中的“值”实际上是属性名。您需要检查:
if(this.detail[value]==null)
你真正想要的是:
const detailValues=Object.values(this.detail);
for(DetailValue的常量值){
console.log(值)
如果(值===null){
log('null found');//我应该在控制台中看到这个,但我没有看到
这是我的国家({
警告消息:“检查输入”
});
打破
}
}
for循环中的“值”实际上是属性名。您需要检查:
if(this.detail[value]==null)
你真正想要的是:
const detailValues=Object.values(this.detail);
for(DetailValue的常量值){
console.log(值)
如果(值===null){
log('null found');//我应该在控制台中看到这个,但我没有看到
这是我的国家({
警告消息:“检查输入”
});
打破
}
}
您答案中的for..不起作用。您需要for(Object.values的const value(this.detail))
@Jolleyboy您的权利,for of不起作用。感谢一定的性能我的错误,对象不能直接使用的的,感谢你答案中的。您需要for(Object.values的const value(this.detail))
@Jolleyboy您的权利,for of不起作用。感谢您的肯定性能我的错误,对象不能直接使用的