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

298
Visualizações
JS React App: array.find not working as expected

I'm trying to complete exercise 2.7 of the FSopen class

I am trying to create a simple phonebook, which can store an array of 'person' objects that have a 'name' attribute. Here is how the data is structured:

 persons = { name: 'Arto Hellas'}

When the user enters the name (that they want to add to the phonebook) into the form input field and presses submit, it triggers an event that first checks if that name already exists in the contact list. Here is the code for that:

  const addPerson = (event) => {
    event.preventDefault()
    
    const personObj = {name: newName}

    const exists = persons.find(n => n.name === newName)

    {(exists === newName) ? alert('name already exists') : setPersons(persons.concat(personObj))}
    
    setNewName('')
    
  }

My issue is that the (exists === newName) statement always returns false, even when the two variables are identical. Resulting in the person being added to the contact list even if a copy already exists. I'm not sure what the issue is here. Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
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