简体   繁体   English

JS/如何通过名字和姓氏输入进行搜索,但如果我在名字后键入空格也可以工作

[英]JS/ How to search with input by firstname and lastname but also work if i type space after firstname

let input = document.getElementById('searchInput')
let searchField = document.getElementById('searchField')

input.addEventListener('keyup', (e) => {
    const string = e.target.value
const filteredUsers = users.filter((user) => {
if (string != '') {
if (user.name.first.toLowerCase().includes(string) || user.name.last.toLowerCase().includes(string)) {
 return user
 } else {
            searchField.innerHTML = ''
        }
    } else {
        searchField.innerHTML = ''
    }
})
filteredUsers.map(user => {
    personImg = user.picture.thumbnail
    person = user.name.first + ' ' + user.name.last
    searchField.innerHTML += `<li class="list-group-item"><img src='${personImg}'>${person}</li>`

})})

I have managed to write a searching function which returns a fetched user picture, first name and last name.我设法编写了一个搜索 function,它返回获取的用户图片、名字和姓氏。 Searching is by first and last name.搜索是按名字和姓氏进行的。 However after entering first name and if i have multiple options displayed, if i press space to enter a last name of my choice it breaks.但是,在输入名字后,如果我显示了多个选项,如果我按空格键输入我选择的姓氏,它就会中断。 How do i implement it to work?我如何实施它才能工作?

Replace this:替换这个:

const string = e.target.value
if (user.name.first.toLowerCase().includes(string) || user.name.last.toLowerCase().includes(string)) {

With this:有了这个:

const q1 = e.target.value.split(' ')[0]
const q2 = e.target.value.split(' ')[1]
if (user.name.first.toLowerCase().includes(q1) && (!q2 || user.name.last.toLowerCase().includes(q2))) {}

This way, it split John Doe to John and Doe and search John in the first names and Doe in the last names.这样,它将John Doe拆分为JohnDoe ,并在名字中搜索John ,在姓氏中搜索Doe ( !q2 || is for when the search query just contains the first name.) !q2 ||适用于搜索查询仅包含名字的情况。)

Here is more completed answer:这是更完整的答案:

// mock user data
let user = {name: {first: 'John', last: 'Doe'}}
// mock event
let e = {target: {value: 'John D'}}
// searched queries
const q1 = e.target.value.toLowerCase().split(' ')[0]
const q2 = e.target.value.toLowerCase().split(' ')[1]
let matched = false;
const first = user.name.first.toLowerCase();
const last = user.name.last.toLowerCase();
if(!q2){
  // when we entered just `John`,
  // then we should match all first names or last names containing `John`
  if(first.includes(q1) || last.includes(q1)) matched = true;
}else{
  // when we entered just `John D`, first name should match `John` and last name should match `D`.
  if(first.includes(q1) && last.includes(q2)) matched = true;
}
if (matched) {}

Also If the names may have more chunks (like: John Doe Smith ), you can split the search string into chunks, and search them inside of names, and sort the results.此外,如果名称可能有更多块(如: John Doe Smith ),您可以将搜索字符串拆分为块,并在名称内搜索它们,并对结果进行排序。 something like:就像是:

let foundUser = undefined;
e.target.value.split(' ').forEach(q => {
  if (user.name.first.toLowerCase().includes(q) || 
      user.name.last.toLowerCase().includes(q)) {
        foundUser = user;
  }
})
if(foundUser){...}else{...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM