Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
How can I find an empty string in this data structure?

I need that when a button is pressed, a state check is made, and depending on the result, one thing or another is done. Basically, check that there is no empty "value" field, and in that case redirect to another page of the application. With code it looks better:

const [formData, setFormData] = useState<ISignUpFormData>({
    username: { value: "", valid: true, unique: true },
    email: { value: "", valid: true, unique: true },
    password: { value: "", valid: true },
    birthdate: { value: "", valid: true },
  });

const handleClick = async (e:any) => {
    let canContinue = true;
    const keys = Object.keys(formData);
    keys.forEach(key => console.log()) // ??? keys is an array of strings, i need to access to "username".value to check if it is "" o not
  }

The value fields are filled by controlled inputs

My question is: how do I loop through the keys of an object whose values ​​are another object?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Object.entries to get key and value and then check if any of the value has empty string.

Some naive implementation below.

Refer to this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

 const object1 = {
    username: { value: "", valid: true, unique: true },
    email: { value: "", valid: true, unique: true },
    password: { value: "", valid: true },
    birthdate: { value: "", valid: true },
  }

let hasEmptyString = false;
for (const [key, formKey] of Object.entries(object1)) {
  if(formKey.value === ""){
    hasEmptyString = true
    break
  }

}
if(!hasEmptyString){
  // redirect to 
}

console.log(hasEmptyString)

about 4 years ago · Juan Pablo Isaza Report

0

You can use Object.values() and find() ... it will return undefined if there is not entry with an empty string

const [formData, setFormData] = useState<ISignUpFormData>({
   username: { value: "", valid: true, unique: true },
   email: { value: "", valid: true, unique: true },
   password: { value: "", valid: true },
   birthdate: { value: "", valid: true },
})

const handleClick = () => {
   const hasEmptyString = Object.values(formData).find(obj => obj.value === '');
   //Returns object with empty string id there is any
   //returns undefined if there is not entry with empty string
   console.log(hasEmptyString);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!