下面是 API 调用返回的 JSON 正文。我正在使用 Postman,并希望使用 JavaScript 创建一个测试来计算返回的 JSON 中的对象(“id
”)数量。 tests["Only 1 login"] =objects=1
之类的内容是 PASS
,否则 Fail
。
[
{
"id": 243,
"user_id": 76,
"account_id": 1,
"unique_id": "12345",
"special_user_id": null,
},
{
"id": 244,
"user_id": 84,
"account_id": 1,
"unique_id": "123456",
"special_user_id": "staff_123456",
}
]
最佳答案
或者只是读取数组时的计数
var count = 0;
ids.forEach(x => {if (x.id != undefined) count++});
console.log(count); // 2
关于javascript - 如何计算数组中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38597327/