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

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

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

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 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