【问题标题】:Postman - Looping through an array of nested objects to make a variablePostman - 循环遍历嵌套对象数组以创建变量
【发布时间】:2021-05-20 08:57:46
【问题描述】:

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

这样做非常简单:

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": "mandatory_field@example.com"
        }
      ],
      "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
  }
}

【问题讨论】:

    标签: javascript arrays loops testing postman


    【解决方案1】:

    您可以使用 danny 提到的 forEach 或 _.each 来获取所有其他使用的数字:

    console.info(jsonData.contacts.find((a)=>a.first_name==="Jacky").phone_numbers[0].value)
    

    使用 array.find 找到 first_name jacky adn 的联系人,然后从中获取 phone_number[0].value。

    如果您想要该数组中的所有数字,请使用:

    console.info(jsonData.contacts.find((a)=>a.first_name==="Jacky").phone_numbers.map((a)=>a.value))
    

    这里我们映射结果以仅从 phone_number 数组中获取数字。

    是你要找的吗!?

    【讨论】:

    • 多种不同的方式来做同样的事情。可能需要修改您的答案以将其推送到变量中,因为这是最初的原因,而不是将其写入控制台。
    • 不用担心,在控制台中检查日志后,一切都变得有意义,我能够设置变量。再次感谢
    【解决方案2】:

    你可以像这样使用一些基本的东西:

    _.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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多