我在javascript中使用for循环对象,但有一个条件是产生问题
我有这个验证器功能我在javascript中使用for循环对象,但有一个条件是产生问题,javascript,json,reactjs,for-loop,if-statement,Javascript,Json,Reactjs,For Loop,If Statement,我有这个验证器功能 import validator from "validator"; import isEmpty from "is-empty"; export default function validate(data) { const errors = {}; for (const property in data) { console.log(property); //<---This is executed
import validator from "validator";
import isEmpty from "is-empty";
export default function validate(data) {
const errors = {};
for (const property in data) {
console.log(property); //<---This is executed
//exclude optional properties from validation
if (!property === "address2") {
console.log("i got called"); //<---but this is not
//return error if any of the compulsory fields is empty
if (isEmpty(data[property])) errors[property] = `${property} is required`;
//phone number validation
if (property === "number")
if (validator.isMobilePhone(data[property], "any"))
errors[property] = "please enter valid phone number";
//return error if country is not Pakistan
if (!property === "country")
if (
data[property].charAt[0].toUpperCase() + //convert first letter to uppercase
data[property].slice(1).toLowerCase() !== //remaining to lowercase
"Pakistan"
)
errors[
property
] = `we care currently taking orders from only within Pakistan`;
}
}
return {
errors,
isValid: isEmpty(errors),
};
}
所有字段均为空,但返回的错误对象仍然为空。这里的任何帮助都是感激的。谢谢你能试试如果(属性!=“address2”)
或者如果(!(属性==“address2”)
你能试试如果(属性!=“address2”)
或者如果(!(属性==“address2”)
我会改变吗
if (!property === "address2")
到
因为您将属性等同于address2,address2当然不是布尔值,如果我的假设正确,属性也不是布尔值。我会改变
if (!property === "address2")
到
因为您将属性等同于address2,address2当然不是布尔值,如果我的假设正确,属性也不是布尔值。您能试试属性吗!==“地址2”?你是对的。您可以将此作为答案发布。我会接受的。谢谢。你能试试酒店吗!==“地址2”?你是对的。您可以将此作为答案发布。我会接受的。是的,我犯了一个愚蠢的错误谢谢我犯了一个愚蠢的错误谢谢
if (!property === "address2")
if (property !== "address2")