1

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?

Community
  • 1
  • 1
Ali
  • 243
  • 1
  • 5
  • 16
  • 3
    *"Count of array could be 100 and it can be updated every 5-10 seconds!"* I think you underestimate the power of modern computing. – Waleed Khan Jan 17 '13 at 17:24
  • There is no `array` function in javascript – Bergi Jan 17 '13 at 17:35
  • @WaleedKhan No didn't. If for comparison try to be used nested loop, the amount of comparison should be mentioned. Especially when the amount of arrays should be bigger (ex: 500*500=250,000 comparisons) and of course for mobile web application. – Ali Jan 25 '13 at 12:30

3 Answers3

0

There is an syntax error in both your functions. Instead of array2.indexOf(array1[i]==-1) it should be array2.indexOf(array1[i])==-1.

To update your array, you can use the unshift (prepending) and splice methods of the array. However, when you remove an item from the array, its length changes which influences your loop condition - better iterate backwards.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Not sure I understand correctly but is this what you need? You want to update and array by keeping as fixed as possible the old element positions, and inserting new elements to the left?

Give it a try here: http://jsfiddle.net/Rt5Mx/

...

If you need to add new elements to the right instead, just change to

      return array3.concat(array2);

and may use array2.reverse() if you need to reverse sort (but you may better consider changing its direction when/where array2 is generated).

UPDATE

Version that keeps also the old array2 intact: with left and right merge:

http://jsfiddle.net/Rt5Mx/2/

  var array1 = [1,5,50,4,12,3,60];
  var array2 = [12,1,5,4,43,3,60];


  alert(mySpecialMergeLeft(array1,array2));
  // 43,1,5,4,12,3,60
  // same as old array1 with added 43 at left and removed 50

  alert(mySpecialMergeRight(array1,array2));
  // 1,5,4,12,3,60,43
  // same as old array1 with added 43 at right and removed 50

  function mySpecialMergeLeft(array1,array2) {
      var pos;
      var array3=[];
      var array4=array2.slice();
      for(var i=0;i<array1.length;i++) {
          pos=array4.indexOf(array1[i]);
        if(pos!=-1) {
          array3.push(array1[i]);
          array4.splice(pos,1);
        }
      }
      return array4.concat(array3);
  }

  function mySpecialMergeRight(array1,array2) {
      var pos;
      var array3=[];
      var array4=array2.slice();
      for(var i=0;i<array1.length;i++) {
          pos=array4.indexOf(array1[i]);
        if(pos!=-1) {
          array3.push(array1[i]);
          array4.splice(pos,1);
        }
      }
      return array3.concat(array4);
  }
FrancescoMM
  • 2,845
  • 1
  • 18
  • 29
  • I don't want to replace current array with new merged array because value of array1 are displayed in user interface so if it replaced with a new values users will be notice these big changes for all values. – Ali Jan 21 '13 at 09:56
  • @Ali if you keep same values at same position what is the problem? Have you tried the code? – FrancescoMM Jan 22 '13 at 17:19
0

This is my solution.

Firstly, I've been using difference api of lodash to save time.

For example: I have a old array contains values 1, 2, 3, and a new array with 2, 4. So I need to remove 1, 3 and add 4 into old array.

const _ = require('lodash');    

const oldArr = [1, 2, 3];
const newArr = [2, 4];

const willRemove = _.difference(oldArr, newArr);
const willAdd = _.difference(newArr, oldArr);

console.log('Remove', willRemove); // [1, 3]
console.log('Add', willAdd); // [4]

Next step is yours.

Long Nguyen
  • 9,898
  • 5
  • 53
  • 52