为什么我用嵌套for循环得到一个奇怪的结果?

时间:2016-02-13 01:59:05

标签: javascript json

我有一些JSON数据,我试图通过使用一些嵌套的for循环来处理。我得到的结果不是我所期望的。以下是我的开始:

data = [
{
    "user_id": "54036a265b2fa765099af857",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5589fdbbe1231a40765a71fe",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
},
{
    "user_id": "5589fdbbe1231a40765a71fe",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
},
{
    "user_id": "5403cffdbd73cc4e0b49bda7",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
}
]

现在,这是我用来处理上述JSON的代码:

for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].days.length; j++) {
        for (var k = 0; k < data[i].days[j].items.length; k++) {
            if (data[i].user_id === data[i].days[j].items[k].user_id) {
                data[i].days[j].item_count++;
            }
        }
    }
}
console.log('result = ' + JSON.stringify(data);

我期待的是item_count将包含一个整数,表示当前用户的项目数(由data[i].user_id给出)。相反,这是我得到的:

result = [
{
    "user_id": "54036a265b2fa765099af857",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 1
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5589fdbbe1231a40765a71fe",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 4
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
},
{
    "user_id": "5589fdbbe1231a40765a71fe",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 1
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 4
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
},
{
    "user_id": "5403cffdbd73cc4e0b49bda7",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 1
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 4
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
}
]

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我运行了你的代码,结果是对的。您可能遇到缓存问题或类似的问题:

  <html>
  <head>


  <script>
  var data = [
  {
    "user_id": "54036a265b2fa765099af857",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5589fdbbe1231a40765a71fe",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
  },
  {
    "user_id": "5589fdbbe1231a40765a71fe",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
  },
  {
    "user_id": "5403cffdbd73cc4e0b49bda7",
    "days": [
        {
            "date": "2016-01-14T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-01-15T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "5608a7b0488505351891c91f"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-01-16T05:00:00.000Z",
            "items": [],
            "item_count": 0
        },
        {
            "date": "2016-02-08T05:00:00.000Z",
            "items": [
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "54036a265b2fa765099af857",
                    "item_id": "56b26ff70ac237b50d061578"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b119409736e0086c37ed0a"
                },
                {
                    "user_id": "5403cffdbd73cc4e0b49bda7",
                    "item_id": "56b26ff70ac237b50d061578"
                }
            ],
            "item_count": 0
        },
        {
            "date": "2016-02-09T05:00:00.000Z",
            "items": [],
            "item_count": 0
        }
    ]
  }
  ];

  for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].days.length; j++) {
        for (var k = 0; k < data[i].days[j].items.length; k++) {
            if (data[i].user_id === data[i].days[j].items[k].user_id) {
                data[i].days[j].item_count++;
            }
        }
    }
  }

  console.log('result = ' + JSON.stringify(data));

  </script>

  </head>
  <body>
  </body>
  </html>