简体   繁体   中英

array or object which one is adapt for live chat using node js

I am learning about nodejs + express + socketio . I am making live chat for customer. I have successfully get user list in Admin page. currently Admin page show all user sending message but I need to show only selected user message from user list.

In this case I need one array or object which have all details about chat session but in my case details are coming from different events so I confused how to push the value.

When I search about array and object I found Arrays have order, objects don't but the statement is confusing me. upto now I can use object but I could not felt any difference between object and array except syntax.

Here I put two questions

  1. Which one right for handle private chat (Array or object)?
  2. How to push the data uniformly from different events for example after triggering three events my object or array should like below

     chat={"name":"Bilal","mail":"test@test.com","socketid":"asgd","to":"John","message":"Hello!","ip":"192.168.1.4","time":"timestamp"}; 

Okay so first let me tell you there is a lot you still need to read up about nodejs and programming in general to start with this project or it will confuse you.

Coming back to your question, the distinction between array and object in terms of having order is that - In simple terms think of "having order" as being able to number the items and retrieve them using an index. This is possible with arrays but not with objects. For example you have an array a and object o , a[5] will give you the 6th element of a but o[5] doesn't make any sense. An object just holds pieces together in no specific order.

IMHO its best to communicate between methods using objects especially in nodejs. You also have the flexibility to use other complex structures when working with objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM