Search by full name - Reactjs

Asked

Viewed 49 times

-2

Hello, I’m trying to create a Search Component and when I search for example full name it return me the full name, but it only returns me the name example: [I type] Victor - He returns Victor, but if I type "Victor Santos" He doesn’t call me back. In short, I would like to know if Regexp can return the full name

function onSearch(e: any) {
  const search = e.target.value
  const searchFilter = myhook.data.filter((item: any) => {
    return !item.name.search(new RegExp(search, 'gi'))
  })
}

  • Victor, there is a lot of information missing in your example code, people who will try to help you will not be able to reproduce your scenario, I suggest you set an example that works and that reproduces your error in the code attached to the question

2 answers

0


test the array using regex like this:

let name = ["Vitor Santos", "Vitor silva", "antonia carolina"];
let stringTest = 'vitor';

// now let’s test and bring the results in a simpler way

  let result = name.map(nam => { 
   if (RegExp(stringTest.toLowerCase()).test(nam.toLowerCase())) {
    return nam; 
   }
  })

// it will bring the array fields that have at least part of the test string and we convert all the strings to minuscule

  • If I change the variable Let stringTeste for Let stringTest = e.target.value it works properly

0

So personally Cláudio Silva, gave me a help and I understood better how to do below is how the function this.

function onSearch(e: any) {
  const search = e.target.value
  const ScriptFilter = searchs.data.filter((item: any) => {
    if (RegExp(search.toLowerCase()).test(item.pdv.name.toLowerCase())) {
      return item
    }
  })

Now I can filter my data and return it complete

Browser other questions tagged

You are not signed in. Login or sign up in order to post.