javascript - Postman - 循环遍历嵌套对象数组以创建变量

标签 javascript arrays loops testing postman

我正在尝试从以下电话号码设置一个变量,其值:“+33652556777”(下面附有 JSON 中的索引 4),这是联系人中的最后一个对象(索引 4)。

这样做非常简单:

let jsonData = pm.response.json();
console.log (jsonData.contacts[4].phone_numbers[0].value)  
const PhoneNumber = jsonData.contacts[4].phone_numbers[0].value
pm.environment.set("Jacky", PhoneNumber);

因为我必须使用不同的查询参数来过滤,例如。 created_at=asc, desc,phone_numbers订单的属性可能会更改索引号,我将无法获取所需的电话号码“+33652556777”,而是会设置我不允许使用不同的电话号码。

我知道有一种方法可以获取我们的号码并使其成为下一个请求的变量,即迭代对象“for….in 或 for…of ”中的属性或键,但对于某些我无法实现它的原因。

我能实现的是获取第一个对象“contacts”,但无法获取其嵌套数组“phone_numbers”。我是这样做的:

let jsonData = pm.response.json();
let contact;
for (let filter of jsonData.contacts){
if (filter.last_name == "Rowland"){
   contact = filter;
}} 
console.log (contact);

你能帮忙吗?

这是 JSON 正文响应:

{
  "contacts": [
    {
      "id": 11121211,
      "direct_link": "https://example.example",
      "first_name": "test1",
      "last_name": "test",
      "company_name": "test",
      "information": null,
      "is_shared": true,
      "created_at": 1582798926,
      "updated_at": 1582798926,
      "emails": [],
      "phone_numbers": [
        {
          "id": 60065270,
          "label": "Work",
          "value": "+33134567666"
        }
      ]
    },
    {
      "id": 22222222,
      "direct_link": "https://example.example",
      "first_name": null,
      "last_name": null,
      "company_name": null,
      "information": null,
      "is_shared": true,
      "created_at": 1583686067,
      "updated_at": 1583686067,
      "emails": [],
      "phone_numbers": [
        {
          "id": 22266444,
          "label": "Work",
          "value": "+33134567899"
        }
      ]
    },
    {
      "id": 33333564,
      "direct_link": "https://example.example",
      "first_name": "Jessica",
      "last_name": "Biel",
      "company_name": "N-Sync",
      "information": null,
      "is_shared": true,
      "created_at": 1583686086,
      "updated_at": 1583686086,
      "emails": [],
      "phone_numbers": []
    },
    {
      "id": 45678901,
      "direct_link": "https://example.example",
      "first_name": null,
      "last_name": null,
      "company_name": null,
      "information": null,
      "is_shared": true,
      "created_at": 1583686105,
      "updated_at": 1583686105,
      "emails": [],
      "phone_numbers": [
        {
          "id": 22266444,
          "label": "Work",
          "value": "+33134567333"
        }
      ]
    },
    {
      "id": 56789123,
      "direct_link": "https://example.example",
      "first_name": "Jacky",
      "last_name": "Rowland",
      "company_name": "Test Company1",
      "information": "",
      "is_shared": true,
      "created_at": 1583745888,
      "updated_at": 1608556499,
      "emails": [
        {
          "id": 76594398,
          "label": "Work",
          "value": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="214c404f4540554e53587e4748444d45614459404c514d440f424e4c" rel="noreferrer noopener nofollow">[email protected]</a>"
        }
      ],
      "phone_numbers": [
        {
          "id": 60650277,
          "label": "Mobile",
          "value": "+33652556777"
        }
      ]
    }
  ],
  "meta": {
    "count": 6,
    "total": 241,
    "current_page": 1,
    "per_page": 5,
    "next_page_link": "https://example.example",
    "previous_page_link": null
  }
}

最佳答案

你可以使用像这样的基本内容:

_.each(pm.response.json().contacts, (contact) => {
    if(contact.last_name === "Rowland") {
        pm.environment.set(`${contact.first_name}_${contact.last_name}_number`, contact.phone_numbers[0].value)
    }
})

可能有更好、更高效的方法来做到这一点,但如果您只想为该联系人设置一个变量,无论他们在响应中的位置 - 这会起作用:D

关于javascript - Postman - 循环遍历嵌套对象数组以创建变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66242482/

相关文章:

javascript - 为什么这段 JavaScript 从未被执行过?

java - 使用多个索引对 String[] 的 ArrayList 进行排序

c - 递归函数,它接受一个数组并以相反的顺序显示元素

javascript - 将元素添加到已评估的循环中

C程序,终端没有打印

javascript - 选择 Yes No 确认 ASP.NET 中的弹出窗口

javascript - jQuery - 使用 Window.Open() 防止弹出窗口阻止程序;

javascript - 直接调用 AngularJS 上工厂实例化的对象

jquery - JSON 数据到 MVC Controller 操作结果

python - 如何像 Matlab parfor 一样并行化 Python 中的 for 循环?