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 forquery.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
4 Answers
Reset to default 3Use 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条)