下面是我对 postman 的 API 请求的 JSON 响应。我希望有一个循环的函数,因为我将在其他请求中使用该函数来根据匹配条件选取某些对象,并在下一个请求中使用它们的属性作为变量。在下面的代码中,我循环遍历响应,如果对象匹配条件,我将其索引推入数组中。我可以从数组中获取对象并在后续请求中使用其参数作为变量。
API JSON 响应
{
"axes": [
{
"id": "66ce0ert63f2982cef2e433b0aa6",
"name": "Product 1",
"max_number_of_days": 5,
"tag": "Measure NPS",
"is_validation_required": false,
"additionnal_day_allowed": true,
"is_pause_days_acquired_by_month": false,
"used_days": 0,
"booked_days": 0,
"remaining_days": 5,
"status": false,
"icon": "provider_pause/stringxoNx.png",
},
{
"id": "66d4e954b0f85d2ewe1b087ad408",
"name": "Product 2",
"max_number_of_days": 54,
"tag": "Get App Store Ratings",
"is_validation_required": true,
"additionnal_day_allowed": false,
"is_pause_days_acquired_by_month": true,
"used_days": 0,
"booked_days": 0,
"remaining_days": 0,
"status": false,
},
{
"id": "67039866e21c6dfwt035a9064ed0",
"name": "Product 3",
"max_number_of_days": 1,
"tag": "Garage Pause",
"is_validation_required": false,
"additionnal_day_allowed": true,
"is_pause_days_acquired_by_month": true,
"used_days": 0,
"booked_days": 0,
"remaining_days": 0,
"status": false,
},
{
"id": "6703f016e21c603wbf5a90ebbbe",
"name": "Product 4",
"max_number_of_days": 10,
"tag": "Garage Pause",
"is_validation_required": true,
"additionnal_day_allowed": true,
"is_pause_days_acquired_by_month": false,
"used_days": 0,
"booked_days": 0,
"remaining_days": 10,
"status": false,
"icon": "product_images/c1WH.jpg",
},
{
"id": "6703fed7e21sdswc6035a9103ac3",
"name": "Product 5",
"max_number_of_days": 5,
"tag": "Garage Pause",
"is_validation_required": false,
"additionnal_day_allowed": true,
"is_pause_days_acquired_by_month": true,
"used_days": 0,
"booked_days": 0,
"remaining_days": 0,
"status": false,
"icon": "product_images/c1WH.jpg",
},
{
"id": "6703fedae21c6035asd9103ac8",
"name": "Product 6",
"max_number_of_days": 5,
"tag": "Garage Pause",
"is_validation_required": false,
"additionnal_day_allowed": true,
"is_pause_days_acquired_by_month": false,
"used_days": 0,
"booked_days": 0,
"remaining_days": 5,
"status": false,
"icon": "provider_pause/Sick Leave36cEFP.png"
}
]
}
//Here is my function
function additionalDayAllowed(jsonData){
let permitted_objects = [];
let number_of_axes = jsonData.axes.length;
let isValidation = jsonData.axes[i].is_validation_required;
let isAdditional = jsonData.axes[i].additionalDayAllowed;
let isRemaining = jsonData.axes[i].remaining_days;
for (let i = 0; i < number_of_axes; i++)
{
If ( isValidation === false && isAdditional === true && isRemaining > 0)
{
permitted_objects.push(i)
}
}
return permitted_objects;
}
function additionalDayAllowed(jsonData){
let permitted_objects = [];
let number_of_axes = jsonData.axes.length;
let isValidation = jsonData.axes[i].is_validation_required;
let isAdditional = jsonData.axes[i].additionalDayAllowed;
let isRemaining = jsonData.axes[i].remaining_days;
for (let i = 0; i < number_of_axes; i++)
{
If ( isValidation === false && isAdditional === true && isRemaining > 0)
{
permitted_objects.push(i)
}
}
return permitted_objects;
}
//Which I tried consuming in a postman sandbox as below;
pm.test("validations", function () {
var jsonData = pm.response.json();
let myExtraDays = extraDaysPermitted(jsonData);
console.log(myExtraDays[0]);
});
您的函数存在一些问题,例如 if 的大写不正确以及循环内轴数组的访问不正确。下面是函数的更正版本,以及简化版本,可帮助您循环响应并查找满足条件的对象。
function additionalDayAllowed(jsonData) {
let permitted_objects = [];
let number_of_axes = jsonData.axes.length;
for (let i = 0; i < number_of_axes; i++) {
let isValidation = jsonData.axes[i].is_validation_required;
let isAdditional = jsonData.axes[i].additionnal_day_allowed;
let isRemaining = jsonData.axes[i].remaining_days;
// Check if the conditions are met
if (isValidation === false && isAdditional === true && isRemaining > 0) {
permitted_objects.push(jsonData.axes[i]); // Push the object itself, or
use i for index if needed
}
}
return permitted_objects; // Return array of matching objects
}
邮递员中的用法: 要在 Postman 测试中使用此功能:
pm.test("validations", function () {
var jsonData = pm.response.json();
let myExtraDays = additionalDayAllowed(jsonData); // Call your function
// Check the first permitted object (if any)
if (myExtraDays.length > 0) {
console.log("First permitted object:", myExtraDays[0]);
} else {
console.log("No matching objects found");
}
});