使用for循环时未定义的Json错误

im正在学习JSON,并在循环JSON时遇到问题。 这是我的JSON响应

{
"success": 1,"category": [{
    "id": "1","category": "Editorial","created_at": "2019-11-05 18:10:31","firstname": "abc","lastname": "xyz"
},{
    "id": "2","category": "Sports","created_at": "2019-11-05 19:25:50",{
    "id": "3","category": "Health","created_at": "2019-11-05 19:27:23",{
    "id": "4","category": "Food","created_at": "2019-11-05 19:39:17","lastname": "xyz"
}]}

这是我的循环

for(var i = 0; i <= jsonData.category.length; i++){
      console.log(jsonData.category[i]['firstname']);}

它确实在控制台中打印,但它给了我这个错误

  

无法读取未定义的属性“名字”       在Object.success

hyqgxnxu 回答:使用for循环时未定义的Json错误

使用<代替<=。否则,您将达到未定义的索引。

for(var i = 0; i < jsonData.category.length; i++){
  console.log(jsonData.category[i]['firstname']);
}
本文链接:https://www.f2er.com/3158838.html

大家都在问