我有一个包含以下内容的响应体,它是一个数组。我想验证其中是否存在且仅存在一个“IsPrimary”为 true 且 salePerson 值为 'Chuck Norris' 的项目。
[
{
"districtName": "Austria",
"storeName": "Store6",
"salePerson": "sdfgh",
"isPrimary": false
},
{
"districtName": "Austria",
"storeName": "Store6",
"salePerson": "dfsd",
"isPrimary": false
},
{
"districtName": "Austria",
"storeName": "Store6",
"salePerson": "Chuck Norris",
"isPrimary": true
},
{
"districtName": "Austria",
"storeName": "Store6",
"salePerson": "sdfgh",
"isPrimary": false
}]
我尝试使用过滤器,但遇到了错误:
data = pm.response.body;
let object_to_find = { salePerson: 'Chuck Norris' };
let result2 = data.filter(function (value) {
return value.salePerson === object_to_find.salePerson && value.isPrimary === true;
});
console.log(result2);
错误信息:
Couldn't evaluate the test script:
TypeError: Cannot read properties of undefined (reading 'filter')