【发布时间】:2018-11-23 16:14:32
【问题描述】:
我正在使用邮递员进行 API 测试。 我的回复信息是
[
{
"firstName": "Jia",
"lastName": "Tes",
"memberId": 745794,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
},
{
"firstName": "Jia",
"lastName": "Test2",
"memberId": 745746,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
},
{
"firstName": "Jia",
"lastName": "Test3",
"memberId": 745748,
"branchId": 12443,
"branchName": "Clubware Mobile Test Branch 2 (Pub)"
},
{
"firstName": "Jia",
"lastName": "Test3",
"memberId": 745745,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
}
]
我想获取“memberId”,其中“firstName”:“Jia”,“lastName”:“Test3”和“branchName”:“NZ - Clubware Mobile Test Branch 1”
我现在的代码是这样的
var response = JSON.parse(responseBody);
console.log("BODY:" + response[3].memberId);
但是我不喜欢使用索引来定位列表中的元素,我该怎么做呢,谢谢大家!!
【问题讨论】:
-
尝试
response.find(e => e.firstName === 'Jia' && e.lastName === 'Test3' && ... etc).memberId- 了解更多关于数组查找方法read the documentation -
@JaromandaX,非常感谢,你的建议很棒!
标签: javascript json postman