简体   繁体   English

返回数组中匹配的对象

[英]return matched objects in array

I'm trying to do a .filter() method on each keyup to get back a list of objects in the following array that match the value of an input field: 我正在尝试对每个按键进行.filter()方法,以获取以下数组中与输入字段值匹配的对象列表:

var peopleArray = [
{
    name: "Steve",
    email: "steve@yahoo.com"
},
{
    name: "Sam",
    email: "sam@yahoo.com"
},
    {
    name: "Sarah",
    email: "sarah@yahoo.com"
},
    {
    name: "Kyle",
    email: "kyle@yahoo.com"
},
    {
    name: "Kody",
    email: "kody@yahoo.com"
},
    {
    name: "Scarlet",
    email: "scarlet@yahoo.com"
},
]

Here is what I'm using to search: 这是我用来搜索的内容:

<input type="search" id="searchInput" placeholder="Search Activities" />

searchInput.addEventListener('keyup', function() {
    searchPeople(searchInput.value);
})


function searchPeople(chars) {

    //Search People and return a list of JSON objects
    return //All JSON objects that matched
}

I'm new to this so I don't quite understand how to achieve this. 我对此并不陌生,所以我不太了解如何实现这一目标。

If you want a simple string search, you would use filter with indexOf to return every string containing the input: 如果要进行简单的字符串搜索,则可以使用带有indexOf filter返回包含输入内容的每个字符串:

function searchPeople(chars) {
  return people.filter(function (person) {
    return person.name.indexOf(chars) > -1;
  });
}

If indexOf finds the string matching chars , it will return a location (index) greater than -1 . 如果indexOf找到与chars匹配的字符串,它将返回大于-1的位置(索引)。 This will match the filter and keep that person. 这将与过滤器匹配并保留该人。

You can expand the filter predicate to check both name and email if you'd like: 如果您愿意,可以展开过滤谓词以检查nameemail

function searchPeople(chars) {
  return people.filter(function (person) {
    return (person.name.indexOf(chars) > -1 || person.email.indexOf(chars) > -1);
  });
}

This will search for people based on name and email. 这将根据姓名和电子邮件搜索人员。 Uppercase/lowercase doesn't matter, because it compares lowercase values to lowercase values. 大写/小写无关紧要,因为它会将小写的值与小写的值进行比较。

filter needs to return a "truthy" value. 过滤器需要返回“真实”值。 So basically it should return anything that is not: false , 0 , undefined , null , NaN or "" (an empty string). 因此,基本上,它应该返回以下内容: false0undefinednullNaN"" (空字符串)。

match will search for a string inside of another string and returns an array of the matched value if it's found, or null if it's not found. match将在另一个字符串中搜索一个字符串,如果找到匹配的值,则返回一个数组;如果找不到,则返回null。 So if match cannot find anything, the return line is essentially return null || null; 因此,如果match找不到任何东西,则返回行本质上将return null || null; return null || null; and since null is "falsy", filter will exclude that person from the result. 并且由于null为“ falsy”,因此过滤器将从结果中排除该人。

function searchPeople(chars) {
    peopleArray.filter(function(person) {
        var name = person.name.toLocaleLowerCase();
        var email = person.email.toLocaleLowerCase();
        var searchTerm = chars.toLocaleLowerCase();
        return name.match(searchTerm) || email.match(searchTerm);
    });
}

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

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