I have the following code which I use in various places
if(!value[field]){
errors[field] = `${FIELDS[field].errorMessage}`;
}
Is it possible to set this as a variable in javascript?
I thought of putting this as a property of an object and calling it from there. Below is my code
_.each(FIELDS, (type, field) => {
if (FIELDS[field].validate) {
if (!value[field]) {
errors[field] = `${FIELDS[field].errorMessage}`;
}
switch (FIELDS[field].formType) {
case "email":
if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value.email)) {
errors.email = 'Invalid email address'
}
default:
if (!value[field]) {
errors[field] = `${FIELDS[field].errorMessage}`;
}
}
}
});
return errors;
}
var isNotPresent = !value[field];and then call it likeif(isNotPresent){...}? is that what you mean?