【发布时间】:2020-04-19 22:57:22
【问题描述】:
我有一个来自端点的 JSON 响应,它给了我一个嵌套的元素数组。给定一个输入值,我想找出该值出现的所有索引值,而不仅仅是元素的第一次出现。
例如,这是我的回复:
{
"items": [
{
"vin": "MMTestingVIN00002",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "OLW",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "HESQM0IBWUR7DH0DU",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "OLW",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "MMTestingVIN00002",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "OLW",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "ZCADWKEQM1GEADEQR",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "WASH",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "H5QGE06R54B8KYOUV",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "WASH",
"type": "S",
"priority": "1"
}
}
}
]
}
我想找出带有"vin = MMTestingVIN00002" 的数组出现的索引。
我查看了https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature 的想法。还查看了其他 SO 答案并尝试过:
* def VIN = 'MMTestingVIN00002'
* def response = result.items
* def names = $[*].vin
* def index = names.indexOf(VIN)
* print index
这给出了索引 0 的唯一第一次出现。理想情况下,我想要一个结果数组 index[],它给出 [0,2] 作为结果。
【问题讨论】:
标签: karate