[英]Passing an Expression as a parameter of a filter function in Typescript
Consider the following Typescript function: 考虑以下Typescript函数:
getPeople(): Person[] {
return model.people;
}
I'd like to implement it with an embedded filter, which will work based on an Expression that I want to pass as a parameter, more or less like this: 我想用一个嵌入式过滤器来实现它,该过滤器将基于我想作为参数传递的Expression起作用,或多或少是这样的:
getPeopleBy(expression): Person[] {
return model.people.filter(expression);
}
var filteredPeople = getPeopleBy(p => p.age < 30);
With Linq and C#, I can do it by accepting a parameter with this syntaxis Expression<Func<EcommerceProduct, bool>> filter
使用Linq和C#,我可以通过使用以下语法接受参数来实现它: Expression<Func<EcommerceProduct, bool>> filter
Is there anything similar in Typescript / Javascript? Typescript / Javascript有什么类似的东西吗?
Disregard (initial answer - leaving it here so people understand the evolution process): 无视(最初的答案-留在这里,以便人们了解进化过程):
Yes, in C# you can do this, but you have to remember TypeScript comes with some sugar syntax that borrows from C#, JavaScript is it's own animal. 是的,在C#中您可以执行此操作,但是您必须记住TypeScript附带了一些糖的语法,这些语法是从C#借用的。
In order to pass an expression, you need to remember that a lamba expression is just a function, so in JS you just have, keys, values (objects) and functions (simple, right?). 为了传递一个表达式,您需要记住,一个lamba表达式只是一个函数,因此在JS中,您只有键,值(对象)和函数(简单,对吧?)。
So to achieve what you want your code should look like this: 因此,要实现所需的代码,应如下所示:
getPeopleBy(expression: Function): Person[] {
return model.people.filter(expression);
}
var filteredPeople = getPeopleBy((p: Person) => { return p.age < 30 });
PS: may I also recommend you change the function name to getPeopleWith ? PS:我是否还建议您将函数名称更改为getPeopleWith ?
as you can see, from a human perspective, it makes much more sense to read: 如您所见,从人类的角度来看,读起来更有意义:
getPeopleWith((p: Person) => { return p.age < 30 });
Basically it's get the people with the age less than 30, easily readable by any person :) 基本上,这使年龄小于30岁的人容易被任何人阅读:)
Update: 更新:
This will offer you the desired result! 这将为您提供所需的结果!
TypeScript Playground Example TypeScript游乐场示例
class People {
private static people: any[] = [];
static where(expression: (value: any, index?: number, Array?: any[]) => boolean):
any[] {
return this.people.filter(expression);
}
}
People.where(p => p.age < 30);
Update 2: 更新2:
TypeScript Playground Example using interface definition for callback 使用接口定义进行回调的TypeScript Playground示例
If you need to write a FluentAPI or something bigger and you're tired of dragging along the callbackfn definition, you can also do something like this: 如果您需要编写FluentAPI或更大的东西,而又不愿意拖拉callbackfn定义,则还可以执行以下操作:
interface IFilter {
value: any;
index?: number;
Array?: any[];
}
class People {
private static people: any[];
static where(expression: (IFilter) => boolean): any[] {
return this.people.filter(expression);
}
}
People.where(p => p.age < 30);
Update 3: 更新3:
TypeScript Playground with Type Inference 具有类型推断功能的TypeScript游乐场
And with this you can also get nice IntelliSense, by using templates in the interface :) 这样,您还可以通过在界面中使用模板来获得不错的IntelliSense :)
interface Person {
age: number;
}
interface IFilter<T> {
value: T;
index?: number;
Array?: T[];
}
class People {
private static people: Person[];
static where(expression: (IFilter: Person) => boolean): any[] {
return this.people.filter(expression);
}
}
People.where(p => p.age < 30);
I hope these series of updates help you achieve your goals. 我希望这些系列的更新可以帮助您实现目标。
Check out linq.js, you can find more info here . 查看linq.js,您可以在此处找到更多信息。
With it you can pass a function as parameter to be used as filter. 有了它,您可以传递一个函数作为参数用作过滤器。
Googling around I found out there is also a TS library (as I see you are using TypeScript), you can find it here . 在Google周围进行搜索,我发现还有一个TS库(如我所见,您正在使用TypeScript),您可以在这里找到它。 I have not tested it anyway :) 反正我还没有测试过:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.