我有一个具有以下模式的记录数组:
apis = [{
info: {
title: 'some title"
}
}]
我需要返回用户输入的是记录标题的所有记录。
我已经使用 Lodash 尝试过类似的操作,但 “title”
始终是一个字母。
this.searchResults = this.apis.filter(function(item){
return _.some(item.info.title, function (title) {
return _.includes(title, query);
});
});
最佳答案
使用 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);
如果要过滤精确匹配,可以:
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);
关于javascript - 如何在对象数组中搜索包含字符串的任何匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49780508/