2016-09-15 36 views
0

我正在嘗試獲取特定頁面的註釋。如何從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; 

它沒有工作。

+0

你檢查[Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)? – raina77ow

+0

你嘗試了什麼? – Slavik

+0

因此,循環,檢查id,如果匹配,將項目推送到數組....(並且不要在您的示例中放置發誓的單詞......) – epascarello

回答

1

改變爲「0Uwx4oXQVEckArRSZNerwhfDZFF3」讓我們假設你的對象稱爲commentList。只需循環對象中的所有鍵並檢查俚語是否匹配,然後推送到數組。

var result = []; 
for(var key in commentList){ 
    if(commentList[key].slangID == theSlangImLookingFor) 
     result.push(commentList[key]); 

} 
0
var obj ={ 
"3d0366ccf32c4459b9a38aee0dde425a": { 
"comment": "This is fucked 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": "fuck 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" 
} 
} 
Object.keys(obj).filter(function(k){ 
    return obj[k].slangID === "34a97a464b51d81a592cf37c8d83cc9e" 
}); 

,因爲我曾用「34a97a464b51d81a592cf37c8d83cc9e」是你提供的數據,在你的折軸

+0

請注意,這將返回匹配註釋的ID而不是註釋本身。然後您需要通過ID查找評論對象。 –

相關問題