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

211
Vistas
Cannot read properties of undefined (reading 'matches')

I am trying to filter through an Array of Objects (Albums in my case), but I am getting this Error and everything I tried did not work.

My Searchbar :

<input type="search" placeholder={" "} onChange={e => onChangeHandler(e.target.value)} value={text}/>

My onChangeHandler

const onChangeHandler =(text) => {
        let matches = []
        if (text.length > 0){
            matches = albums.filter(album => {
                const regex = new RegExp(`${text}`, "gi")
                return (album.artist.matches(regex));
            });
        }
        console.log('matches', matches)
        setSuggestions(matches)
        setText(text)
    }

When I try, it works and I get the matches in the console, but only if I type everything correctly and case sensitive. That is not the goal tho.

return (album.artist === text);
about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

You can use includes method of strings for this. So for your example something like this:

matches = albums.filter(album => album.artist.toLowerCase().includes(text.toLowerCase()));

If you want the search to be case sensitive, you should remove toLowerCase methods.

about 4 years ago · Santiago Gelvez Denunciar

0

There are probably 2 issues in your code. First it doesn't seem to find the album.artist so make sure your object looks correct.

Secondly matches() is not a function on a String and you probably meant match()

let albums = [{
  artist: "Me"
}, {
  artist: "You"
}]

const onChangeHandler = (text) => {
  let matches = []
  if (text.length > 0) {
    matches = albums.filter(album => {
      const regex = new RegExp(`${text}`, "gi")
      return (album.artist.match(regex)); // Here matches => match
    });
  }
  console.log('matches', matches)
  //setSuggestions(matches)
  //setText(text)
}
onChangeHandler("mE")
onChangeHandler("me")
onChangeHandler("yo")

about 4 years ago · Santiago Gelvez Denunciar
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