{
"page": 2,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 7,
"email": "michael.lawson@reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg"
},
{
"id": 8,
"email": "lindsay.ferguson@reqres.in",
"first_name": "Lindsay",
"last_name": "Ferguson",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg"
},
{
"id": 9,
"email": "tobias.funke@reqres.in",
"first_name": "Tobias",
"last_name": "Funke",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg"
},
{
"id": 10,
"email": "byron.fields@reqres.in",
"first_name": "Byron",
"last_name": "Fields",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg"
},
{
"id": 11,
"email": "george.edwards@reqres.in",
"first_name": "George",
"last_name": "Edwards",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg"
},
{
"id": 12,
"email": "rachel.howell@reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg"
}
]
}
上面的是json响应。它具有字段名称“ id,电子邮件,first_name,last_name和化身”。我需要验证每个字段的数量。预期的输出是:id-6,名6,电子邮件6,last_name-6和化身6。而且我不想硬编码,这些字段存在于一个csv文件中。意味着我不想对字段名进行硬编码,我想进行参数化,以便它从csv文件读取数据。这样,即使我必须获得100个字段的计数,代码也应该可以工作。如果需要其他详细信息,请告诉我
答案 0 :(得分:1)
您可以这样简单地计数
console.log(value.data.length)
除非有可能没有id字段,否则您可以这样做
var count = 0;
for (let i =0; i < value.data.length; i++) {
if(value.data[i].id != undefined) count++
}
console.log(count)