【问题标题】:Karate - To find array indices of multiple occurance of an element in a JSON response array空手道 - 在 JSON 响应数组中查找元素多次出现的数组索引
【发布时间】: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


    【解决方案1】:

    给你:

    * def indexes = []
    * def fun = function(x, i){ if (x.vin == 'MMTestingVIN00002') karate.appendTo(indexes, i) }
    * karate.forEach(response.items, fun)
    

    【讨论】:

      猜你喜欢
      • 2013-08-26
      • 2011-04-17
      • 2016-06-09
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      相关资源
      最近更新 更多