In javascript I have two arrays which they have list of online users in a chat room. First array which has old values must be updated with new one.
var array1 = array(1,5,50,4,12,3,60);
var array2 = array(12,1,5,4,3,60);
array1 is on the user interface so I don't want to replace it with the array2. First of all I want to remove values from array1 which are not in the array2 and then add new values from array2 at beginning of array1.
I should also mention that sort of array 2 is important and the count of arrays could be different. Count of array could be 100 and it can be updated every 5-10 seconds!
For an example you can check list of online users on Stackoverflow chat services https://chat.stackoverflow.com/rooms/17/javascript
This is the first way which I may develop it, can I do something better?
For Removing old values
for(i=0;i<array1.length;i++)
if(array2.indexOf(array1[i])==-1)
removeValue(array1[i]);
For adding new values
for(i=0;i<array2.length;i++)
if(array1.indexOf(array2[i])==-1)
addValue(array2[i]);
Do you have any idea for update first array?