gpt4 book ai didi

javascript - 空检查对象不起作用

转载 作者:行者123 更新时间:2023-12-01 02:05:24 25 4
gpt4 key购买 nike

我正在尝试检查空值,即使我知道这些值为空,循环仍然不会中断。为任何帮助干杯

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 循环中的“值”实际上是属性名称。您需要检查:if (this.detail[value] === null)

你真正想要的是:

const detailValues = Object.values(this.detail);

for (const value of detailValues) {
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;
}
}

关于javascript - 空检查对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com