0

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);
});

3 Answers3

2

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);
});
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())
Rcdlb
  • 91
  • 4
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