Explore the common issues related to object iteration and queries outlined below.
Presented here is a list of objects (used for managing a connection form):
const connectionData = {
mail: {
value: false,
isRequired: true
},
password: {
value: false,
isRequired: true
},
name: {
value: false,
isRequired: false
},
}
Included below is an array variable that contains all the objects from connectionData
const fieldsToCheck = [connectionData.mail, connectionData.password, connectionData.name]
The intention of the check
function below is to verify if:
For each object in fieldsToCheck
where isRequired
is true, if value
is false, return true, otherwise, return false
const check = () => {
if((fieldsToCheck.filter(key =>
key.isRequired).every(val =>
!val.value))) {
return true
} else {
return false
}
}
This function adequately serves its purpose. With the current object values, it returns true
, and false
if values
are altered to true
or ""
I pondered if there might be a quicker or more refined method of achieving the same outcome.
Wishing everyone a pleasant day ahead!