Home > Back-end >  How to use check multiple object properties
How to use check multiple object properties

Time:09-27

I have an if-statement: if all the properties of an object have values, then the next button will be enabled.

const handleButton = () => {
  if(
        values.streetAdress !== ""
     && values.postalCode !== ""
     && values.city !== ""
     && values.sex !== ""
     && values.birthName !== ""
     && values.birthPlace !== ""
     && values.birthday !== ""
     && values.country !== ""
     && values.family !== ""
     && values.insuranceID !== ""
  ){
    setDisabeld(false)
  }else{
    setDisabeld(true)
  }

Is there a way to shorten this expression, so all properties will be checked at once?

CodePudding user response:

you can use the Array.prototype.every() method on the values of the object.

const isNotEmpty = Object.values(values).every((v) => v !== '')
if(isNotEmpty) {
    setDisabeld(false)
} else{
    setDisabeld(true)
}
  • Related