简体   繁体   English

如何在对象数组中搜索包含字符串的所有匹配项?

[英]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.

相关问题 如何在 typescript 中将任意数组转换为包含其他数据类型的字符串数组? - How do I convert an any array to a string array containing other datatypes inside in typescript? 如何对姓氏进行排序、格式化电话号码并返回包含对象数组中的值的字符串? - How do I sort the last names , format phone numbers, and return a string containing values from an array of objects? 如何在 object 中的数组中搜索指定字符串值的任何实例? - How do I search array within an object for any instances of specified string values? 如何对包含字符串和整数的数组中的元素进行排序? - How do i sort elements in an array, containing both string and integers? 如何在包含许多对象的数组中搜索,然后对其进行更新以不显示重复项 - How can I search in my array containing many objects, then updating it so no duplicates are shown 如何检查对象的值是否与 Angular 中数组列表中的任何值匹配 - How to Check If a Value of Objects Matches Any Value in a List of Array in Angular 如何在Javascript对象数组中搜索特定值? - How do I search for a specific value in an array of objects in Javascript? 通过包含字符串对对象数组进行排序 - Sort array of objects by containing string 我可以对对象数组进行二进制搜索吗? - Can I do a binary search on an array of objects? 在对象数组中搜索匹配项。 JS - Search for matches in an array of objects. JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM