我正在嘗試獲取特定頁面的註釋。如何從javascript對象中獲取與某個ID匹配的項目
所以我想把slangID = 0Uwx4oXQVEckArRSZNerwhfDZFF3的所有註釋都放到數組或對象中。 這裏是我的JavaScript對象:
{
"3d0366ccf32c4459b9a38aee0dde425a": {
"comment": "This is up\nI know",
"commentID": "3d0366ccf32c4459b9a38aee0dde425a",
"slangID": "34a97a464b51d81a592cf37c8d83cc9e",
"time_date": "2016-09-15T06:15:09.969Z",
"user_id": "0Uwx4oXQVEckArRSZNerwhfDZFF3",
"username": "james"
},
"4b0839bba81943e9cb511365d2978ad6": {
"comment": "aaaaaaa",
"commentID": "4b0839bba81943e9cb511365d2978ad6",
"slangID": "34a97a464b51d81a592cf37c8d83cc9e",
"time_date": "2016-09-15T06:54:36.026Z",
"user_id": "bHDKSU6aorX9efzBCHy",
"username": "ericel123"
},
"5064603984a3a217ae02cd0dab7ede81": {
"comment": "this\n",
"commentID": "5064603984a3a217ae02cd0dab7ede81",
"slangID": "34a97a464b51d81a592cf37c8d83cc9e",
"time_date": "2016-09-15T06:37:15.113Z",
"user_id": "0Uwx4oXQVEckArRSZNer",
"username": "james"
},
"59691edb302c4d0a70d6bb860e64d4e1": {
"comment": "hello\n",
"commentID": "59691edb302c4d0a70d6bb860e64d4e1",
"slangID": "34a97a464b51d81a592cf37c8d83cc9e",
"time_date": "2016-09-15T06:36:41.308Z",
"user_id": "0Uwx4oXQVEckArRSZNer",
"username": "james"
}
}
所以我可以把評論中所謂的「意見」的對象。 我試過像這樣的:
var filtered = vm.comments.filter(function(item) {
return item.slangID === sid;
});
var comments = filtered;
它沒有工作。
你檢查[Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)? – raina77ow
你嘗試了什麼? – Slavik
因此,循環,檢查id,如果匹配,將項目推送到數組....(並且不要在您的示例中放置發誓的單詞......) – epascarello