javascript - Search collection in Typescript for partial string match - Stack Overflow

I want to search collection in typescript as we can search in SQL and linq with like operator in sql an

I want to search collection in typescript as we can search in SQL and linq with like operator in sql and contain method in linq i.e in sql

name like ‘%searchString%’

and in linq

query = query.Where(x => x.UserName.ToLower().Contains(queryObj.SearchStr.ToLower()));

Is there a way we can do this operation in typescript? To search a collection of strings for items with partial match?

Thank you

I want to search collection in typescript as we can search in SQL and linq with like operator in sql and contain method in linq i.e in sql

name like ‘%searchString%’

and in linq

query = query.Where(x => x.UserName.ToLower().Contains(queryObj.SearchStr.ToLower()));

Is there a way we can do this operation in typescript? To search a collection of strings for items with partial match?

Thank you

Share Improve this question edited Apr 21, 2020 at 18:22 Arghya C 10.1k4 gold badges49 silver badges69 bronze badges asked Apr 21, 2020 at 18:02 user2825468user2825468 631 silver badge8 bronze badges 10
  • can you provide sample data ? – rksh1997 Commented Apr 21, 2020 at 18:03
  • You may consider any type of data in collection – user2825468 Commented Apr 21, 2020 at 18:05
  • 1 String.prototype.includes() might be what you're looking for query.filter(e => e.includes("searchString")). developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Jason White Commented Apr 21, 2020 at 18:08
  • filter should work – Arghya C Commented Apr 21, 2020 at 18:08
  • 1 @Arghya C, filter with include is working well, thank you – user2825468 Commented Apr 21, 2020 at 18:17
 |  Show 5 more ments

4 Answers 4

Reset to default 3

Use the Javascript filter array function. filter returns the items in an array where the expression evaluates to a truthy result.

You can use the includes string function to see if a string exists inside another string.

const values = [
  'abc',
  'aYz',
  'xyz'
];

const filtered = values.filter(x => x.toLocaleLowerCase().includes('y'));

console.log(filtered);

const arr: string[] = ['paris', 'berlin', 'delhi', 'istanbul'];

const result: string[] = arr.filter(key => key.toLowerCase().includes('in'));

To find one value. The result isn't an array here. It returns berlin.

const values = ['paris', 'berlin', 'delhi', 'istanbul'];

const filtered = values.find(x => x.toLocaleLowerCase().includes('in'));

console.log(filtered);

Linq:

query = query.Where(x => x.UserName.ToLower().Contains(queryObj.SearchStr.ToLower()));

TypeScript/JavaScript

query = query.filter(x => x.userName.toLowerCase().includes(queryObj.SearchStr.toLowerCase()))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745431994a4627433.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信