【问题标题】:Postman Test Array inside nested Array嵌套数组中的邮递员测试数组
【发布时间】:2020-01-04 00:48:33
【问题描述】:

我试图将断言放在邮递员数组中。我试图在数组中检索数组,但它有错误。

我们将不胜感激任何建议。

{
  "meta": {
    "code": 200
  },
  "data": [
    [
      {
        "_index": "test",
        "_type": "test",
        "_id": "sdgsdfg",
        "_version": 1,
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true
      },
      {
        "_index": "test",
        "_type": "test",
        "_id": "test",
        "_version": 1,
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true
      },

    ]
  ]
}

到目前为止,我已经尝试过这个,但它给出了错误。

var jsonData = JSON.parse(responseBody);

pm.test("Checking Success Response on Service", function () {
    pm.response.to.have.status(201);
});

for(var i=0; i< jsonData.data.length ;i++){

    for(var j=0; j< jsonData.data[i].length ;j++){
        console.log(jsonData.data[i].j._index);
    }
}

评估测试脚本时出错:TypeError: Cannot read property '_index' of undefined

请告诉我可以做什么。

【问题讨论】:

  • 嵌套数组所以jsonData.data[i][j]._index
  • 工作正常。但我不明白这里的逻辑,里面有第二个数组。它也有单独的对象。
  • jsonData.data[i].j._index 当你说j 时,它的字面意思是j。您需要说 [j] 以便它可以评估 j 的值,然后搜索数组。
  • 那么你是在告诉我它像矩阵一样工作吗?

标签: javascript arrays json api postman


【解决方案1】:

您可以使用更基本的东西将这些值记录到控制台:

jsonData.data.forEach(dataItem => {
    dataItem.forEach(item => {
        console.log(item._index)
    })
})

这只是循环遍历数据数组,然后循环遍历嵌套数组。

【讨论】:

    猜你喜欢
    • 2015-09-18
    • 2020-11-19
    • 1970-01-01
    • 2021-01-27
    • 2019-09-12
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多