[英]How do I search an array of objects for any matches containing a string?
I have an array of records with the following pattern: 我有一个具有以下模式的记录数组:
apis = [{
info: {
title: 'some title"
}
}]
I need to return all records where the user input is the record's title. 我需要返回所有记录,其中用户输入是记录的标题。
I've tried something like this using Lodash but "title"
is always a single letter. 我已经使用Lodash尝试过类似的操作,但是"title"
始终是一个字母。
this.searchResults = this.apis.filter(function(item){
return _.some(item.info.title, function (title) {
return _.includes(title, query);
});
});
Using ES6 filter
, you can: 使用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);
If you want to filter the exact match, you can: 如果要过滤精确匹配,则可以:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.