[英]Finding an custom element in an array Javascript
我用JavaScript創建了一個類
function chat(data){
this.id = data.id;
this.name = data.name;
this.message = data.message
this.date = data.date;
}
現在,我聲明這樣的數組。
var message = [];
並將我的聊天對象推送到消息數組中;
var chat = new chat(data);
message.push(chat);
通過id在消息數組中查找元素的最佳方法是什么。 或使它更容易的任何其他事情,我還需要使用日期來縮短數組。
環。 由於聊天是對象,因此不能使用Array.indexOf。
如果相反
var Message = {};
Message[data.id]=new chat(data);
如果chat()像您顯示的那樣簡單,那么只需執行
Message[data.id]=data;
然后您可以直接通過ID訪問
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.