[英]How to get particular value from fixture file in cypress
我檢索下面提到的代碼片段作為請求的響應
如果我根據 JSON 響應中的用戶名進行搜索,如何檢索“id”例如 - 當我搜索名稱“Leanne Graham”時,如何獲得“id”值為 1 的響應
注意 響應值及其位置是動態的
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
]
如果用戶名是唯一值,這是一種方法。
let users = YOUR_JSON_LIST;
let idUser = users.find(u => u.username == SOME_USERNAME).id;
如果搜索屬性不限於唯一值,您可以使用 map 獲取值列表
let usersIdList = users
.filter(u => u.website == SOME_WEBSITE)
.map(u => u.id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.