javascript - Create/manage multidimensional array using jQuery -


i building chat application using socket.io.

i need create array manage users connected each chat room.

to start need store chat room name, , in chat room each user , status.

chat1      nickname: user1      status:   admin      nickname: user2      status:   none      nickname: user3      status:   none 

i need sort nicknames in alphabetical order when created or when user added or deleted. possible?

what you've described isn't multidimensional array, it's array of objects or maps. assuming objects:

chat1 = [      { nickname: 'ethelred',        status:   'admin'},      { nickname: 'alfred',        status:   'none' },      { nickname: 'canute',        status:   'none' }   ]  // that's needed simple sort: chat1.sort((a,b) => a.nickname > b.nickname) 

this sort , give [{nickname: 'alfred', status: 'none' }, {nickname: 'canute', status: 'none' }, { nickname: 'ethelred', status: 'admin'}]

// can continue transform data, // example, pull user nicknames: chat1.sort((a,b) => a.nickname - b.nickname)      .map(user => user.nickname) 

that sort , map across resulting sorted array give list of users in alphabetical order - ['alfred','canute','ethelred'].

note, naïve, need better logic in sort function (what happens capital letters, numbers, numbers mixed in letters, &c?). function isolated. write separate (dumb) function knows nothing chatroom object, , pass sort() a.nickname , b.nickname arguments - chat1.sort(myfunkysortfunction(a.nickname, b.nickname)) (or inline logic , don't abstract it, whatever's simplest).

you'll start getting issues if have lot of users, you'll need run on every join/leave event. @ point you'll need lot more robust.


edit1:

so when new person joins, either a. .push()-ed onto original array, or b. create new array new person's object. when person leaves, either a. removed original array, or b. new array created doesn't include them.

when either of events happen, run sort function. need @ array.prototype.sort() method. should literally thing need.

note, i'm suggesting not efficient, job, , simple.


re function passed sort, a.nickname > b.nickname not going work well. a-z come before a-z. 'xerxes' come before 'albert'. do:

chat1.sort(function(a,b) {   return a.nickname.tolowercase() > b.nickname.tolowercase(); }) 

so sort out. you've still issue more complex names: it's worth looking @ robust described here: javascript : natural sort of alphanumerical strings

basically, you're doing isn't hard: need make sure in callback function, specify you're grabbing nickname property, otherwise it'll try sort based on whole object, , return weird incorrect results.


[very late] edit2

i realised asked how add , remove. staying on same (functional) theme rest of above: add can use array.prototype.concat() create new array includes user. remove, array.prototype.filter() return new array specific user excised.

so add:

current = [   { nickname: 'ethelred',     status:   'admin'},   { nickname: 'alfred',     status:   'none' },   { nickname: 'canute',     status:   'none' } ]  current.concat({ nickname: 'offa', status: 'none' })  // [{ nickname: 'ethelred', status: 'admin'}, { nickname: 'alfred', status: 'none'}, { nickname: 'canute', status: 'none'}, { nickname: 'offa', status: 'none'}] 

to remove:

current = [   { nickname: 'ethelred',     status:   'admin'},   { nickname: 'alfred',     status:   'none' },   { nickname: 'canute',     status:   'none' } ]  current.filter({nickname} => nickname !== 'canute')  // [{ nickname: 'ethelred', status: 'admin'}, { nickname: 'alfred', status: 'none'}] 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -