Javascript 将filter()与includes()结合使用以获取部分匹配

Javascript 将filter()与includes()结合使用以获取部分匹配,javascript,arrays,ecmascript-6,lodash,Javascript,Arrays,Ecmascript 6,Lodash,我有一个包含要搜索的对象的数组。可搜索数组如下所示: [ { value: 0, label: 'john' }, { value: 1, label: 'johnny' }, { value: 2, label: 'peter' }, { value: 3, label: 'peterson' } ] 我使用Lodash过滤器方法进行搜索: search = (text) => { let results = _.filter(

我有一个包含要搜索的对象的数组。可搜索数组如下所示:

[
    { value: 0, label: 'john' },
    { value: 1, label: 'johnny' },
    { value: 2, label: 'peter' },
    { value: 3, label: 'peterson' }
]
我使用Lodash过滤器方法进行搜索:

search = (text) => {
    let results = _.filter(
        this.props.options,
        { label: text }
    );
}
这仅显示与搜索查询完全匹配的结果(
text
参数)。我需要用部分匹配来完成这项工作。因此,如果我插入
j
johnny
它应该能够同时找到“John”和“johnny”

我试过:

search = (text) => {
    let results = _.filter(
        this.props.options => 
            this.props.options.includes({ label: text })
    );
}
但是,没有运气。没有错误,也没有结果。我怎样才能做到这一点呢?

接受字符串作为针。如果指针不是字符串,它将转换为字符串,并且对象的大小写为
[object object]

您应该获取
标签的值
,并使用字符串的includes方法:

const选项=[
{value:0,标签:'john'},
{value:1,标签:'johnny'},
{value:2,标签:'peter'},
{值:3,标签:'peterson'}
];
const search=(text)=>options.filter(({label})=>label.includes(text));
常量结果=搜索('jo');
控制台日志(结果)接受字符串作为指针。如果指针不是字符串,它将转换为字符串,并且对象的大小写为
[object object]

您应该获取
标签的值
,并使用字符串的includes方法:

const选项=[
{value:0,标签:'john'},
{value:1,标签:'johnny'},
{value:2,标签:'peter'},
{值:3,标签:'peterson'}
];
const search=(text)=>options.filter(({label})=>label.includes(text));
常量结果=搜索('jo');

控制台日志(结果)由于您使用的是ES6标准的一部分,因此我将使用ES6而不是lodash过滤器来解决此任务:

let search = (list, text) =>
  list.filter(i => i.label.toLowerCase().includes(text.toLowerCase()));

let list = [
    { value: 0, label: 'john' },
    { value: 1, label: 'johnny' },
    { value: 2, label: 'peter' },
    { value: 3, label: 'peterson' }
];

let result = search(list, 'j');

console.log(result); // [{value: 0, label: "john"}, {value: 1, label: "johnny"}]

另外,使用
.toLowerCase
您可以使用“John”而不是“John”。

因为您使用的是ES6标准的一部分
包括
,那么我将使用ES6而不是lodash过滤器来解决此任务:

let search = (list, text) =>
  list.filter(i => i.label.toLowerCase().includes(text.toLowerCase()));

let list = [
    { value: 0, label: 'john' },
    { value: 1, label: 'johnny' },
    { value: 2, label: 'peter' },
    { value: 3, label: 'peterson' }
];

let result = search(list, 'j');

console.log(result); // [{value: 0, label: "john"}, {value: 1, label: "johnny"}]

另外,使用
.toLowerCase
可以使用“John”而不是“John”。

这不是使用
String.prototype.includes的方式。应该为其提供字符串,而不是对象。您应该提供一个函数,将调用包装到
includes

search = (text) => {
    let results = _.filter(
        this.props.options,                             // first parameter to _.filter is the array
        option => option.label.includes(text)           // the second parameter is a funtion that takes an option object and returns a boolean (wether the label of this option includes the text text or not)
    );
}

这不是使用
String.prototype.includes的方式。应该为其提供字符串,而不是对象。您应该提供一个函数,将调用包装到
includes

search = (text) => {
    let results = _.filter(
        this.props.options,                             // first parameter to _.filter is the array
        option => option.label.includes(text)           // the second parameter is a funtion that takes an option object and returns a boolean (wether the label of this option includes the text text or not)
    );
}
像“Sonja”这样的名字怎么样。它会发现这一点,还是你只想要以字母开头的名字
includes
将同时返回这两个名称。类似“Sonja”的名称如何。它会发现这一点,还是你只想要以字母开头的名字
includes
将同时返回这两个值。