Karate 如何使用外部变量筛选贴图列表
我有一个这样的地图列表:Karate 如何使用外部变量筛选贴图列表,karate,Karate,我有一个这样的地图列表: [ { "name": "Marco", "email": "marco@gmail.com", "age": 20 }, { "name": "Polo", "email": "polo@gmail.com", "
[
{
"name": "Marco",
"email": "marco@gmail.com",
"age": 20
}, {
"name": "Polo",
"email": "polo@gmail.com",
"age": 25
}
]
* def filter_func = function(x){ return x.email == "polo@gmail.com" }
* def list = response
* def filtered = karate.filter(list, filter_func)
我只想返回其电子邮件为特定邮件的条目,如polo@gmail.com我通常会这样过滤:
[
{
"name": "Marco",
"email": "marco@gmail.com",
"age": 20
}, {
"name": "Polo",
"email": "polo@gmail.com",
"age": 25
}
]
* def filter_func = function(x){ return x.email == "polo@gmail.com" }
* def list = response
* def filtered = karate.filter(list, filter_func)
但是电子邮件必须是一个变量,因为首先我创建了一个随机帐户,然后我得到了帐户列表,必须检查帐户是否已添加,并在以后使用它的其他参数,如年龄
有没有办法将空手道过滤功能与外部变量或其他策略一起使用?变量用法如下(非工作示例):
没关系,我发现我可以引用函数中的变量,比如:
* def email = "polo@gmail.com"
* def filter_func = function(x){ return x.email == email }
* def list = response
* def filtered = karate.filter(list, filter_func)
给你,简单的JS:
* def email = "polo@gmail.com"
* def fun = function(x){ return x.email == email }
* def filtered = karate.filter(response, fun)
* print filtered
由于fun
是在email
之后声明的,因此变量引用有效
在一些罕见的情况下,如果函数是较早声明的-例如,当您想要实现代码的重用时,请注意,您可以始终使用
karate.get(name)
按变量名获取“当前存在”变量值。我迟到了6秒!您可以将我的答案标记为“已接受”,因为它提供了一些有用的信息:)