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

160
Visualizações
partial search and lowercase search in nodejs

I'm trying to implement multiple search functionality such as userName and userId with separate input for userName and userId. If I give userName as "Dave Steve" or "dave steve" or "Dave" or "steve" or "dav" and click on the search button I should get the result But When I give "Dave steve" it working fine but not with other case How do I implement it ?

Json data:

let logData={
"dataLog":
[
    {
        "userId":"1",
        "userName":"Dave Steve",
    },
    {
        "userId":"2",
        "userName":"John Doe",
    }
],
}

Here is my code

app.get('/getData', (req, res) => {
  let accessData= logData.dataLog;
  let userName = req.query.userName;
  let userId = req.query.userId;
    console.log(userName, userId)
    let filterData = accessData.filter((data) => {
      return (userId === undefined || data.userId === userId) &&
        (userName === undefined || userName === userNameData)
    })
    console.log(filterData)
    const response = {
      header: getSuccessHeader,
      body: filterData
    };
    res.status(200);
    res.send(response);
});
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Please try it

app.get('/getData', (req, res) => {
  let accessData= logData.dataLog;
  let userName = req.query.userName;
  let userId = req.query.userId;
    console.log(userName, userId)
    let filterData = accessData.filter((data) => {
      return (userId === undefined || data.userId === userId) &&
        (userName === undefined || userName.toLowerCase().includes(userNameData.toLowerCase()))
    })
    console.log(filterData)
    const response = {
      header: getSuccessHeader,
      body: filterData
    };
    res.status(200);
    res.send(response);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

It's because Dave Steve isn't really equal to Dave steve when you are comparing strings using ===. It's case sensitive.

You can use .toLowerCase() to both userName and userNameData when comparing the two.

So your comparison would look like:

userName.toLowerCase() === userNameData.toLowerCase()

EDIT:

I completely glossed over that you want partial string comparison. So you should instead use .includes(string) instead.

userName.toLowerCase().includes(userNameData.toLowerCase())
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an implemented stackblitz link for you, go through it.

The comparison need to happen with both cases with an or statement :

userName.toLowerCase() === data.userName.toLowerCase() ||
userName.toLowerCase().indexOf(data.userName.toLowerCase()) === -1
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