Mar*_*ano 1 javascript arrays lodash
我有一个包含以下模式的记录数组:
apis = [{
info: {
title: 'some title"
}
}]
Run Code Online (Sandbox Code Playgroud)
我需要返回用户输入是记录标题的所有记录.
我尝试使用Lodash这样的东西,但"title"总是一个字母.
this.searchResults = this.apis.filter(function(item){
return _.some(item.info.title, function (title) {
return _.includes(title, query);
});
});
Run Code Online (Sandbox Code Playgroud)
使用ES6 filter,您可以:
let apis = [
{info: {title: 'select some title'}},
{info: {title: 'some title 2'}},
{info: {title: 'some title 3'}}
];
let toSearch = 'select'; //Will check if title have text 'search'
let result = apis.filter(o => o.info.title.includes(toSearch));
console.log(result);Run Code Online (Sandbox Code Playgroud)
如果要过滤完全匹配,您可以:
let apis = [
{info: {title: 'select some title'}},
{info: {title: 'some title 2'}},
{info: {title: 'some title 3'}}
];
let toSearch = 'select some title';
let result = apis.filter(o=> o.info.title === toSearch);
console.log(result);Run Code Online (Sandbox Code Playgroud)