Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda