我想基于过滤器从json数组中删除一个对象,尝试在以下代码下尝试但不起作用
* def json = [ { "id": "0a7936ed", "code": "test", "label": "test", "type": "sell" }, { "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" } ]
我要删除代码等于测试的对象
* def fun = function(){ karate.remove('json', $.[?(@.code=='test')]") }
* call fun
获取异常如下:
com.intuit.karate.exception.KarateException: javascript function call failed: String index out of range: -1
at com.intuit.karate.Script.evalFunctionCall(Script.java:1622)
at com.intuit.karate.Script.call(Script.java:1573)
at com.intuit.karate.Script.callAndUpdateConfigAndAlsoVarsIfMapReturned(Script.java:1690)
at com.intuit.karate.StepDefs.callAndUpdateConfigAndVars(StepDefs.java:582)
请建议我如何过滤它并删除对象。 谢谢。。
答案 0 :(得分:2)
此用例正是karate.filter()
设计的目的:
* def json = [ { "id": "0a7936ed", "code": "test", "label": "test", "type": "sell" }, { "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" } ]
* def condition = function(x){ return x.code != 'test' }
* def filtered = karate.filter(json, condition)
* match filtered == [{ "id": "7bc1909b2", "code": "test2", "label": "test2", "type": "Buy" }]