Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda