1

Say I have the following two arrays:

var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];

I'd like to itterate over arrTwo and if it contains an element that is also in arrOne, remove it from arrTwo, and insert it in arrThree. So looking at the above arrays the state of the arrays afterwards should look like this:

var arrOne = [1, 4, 7];
var arrTwo = [2, 3, 5];
var arrThree = [1, 4];

Could anyone point me in the right direction and the best way to go about this? If code is provided, a step-by-step explanation would really be appreciated so that I can understand what's going on.

  • 1
    even simple `for` loop can be used, What is your effort? You can get an idea from your previous question. http://stackoverflow.com/questions/39972283/javascript-filtering-by-comparing-two-arrays – Satpal Nov 11 '16 at 08:32

7 Answers7

5

A simple for loop, matching with indexOf and splicing matches.

var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];
for (var i = 0; i < arrTwo.length; i++) {
  if (arrOne.indexOf(arrTwo[i]) >= 0) {
    arrThree.push(arrTwo[i]);
    arrTwo.splice(i, 1);
    i--;
  }
}

console.log(arrOne, arrTwo, arrThree)

Array.IndexOf

Array.splice

Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
3

Look into the Underscore library. All the elements in arrOne that are also in arrTwo is called _.intersection().

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
2

Use simple while loop with Array#splice and Array#unshift methods.

var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];
// get length of array
var l = arrTwo.length;
// iterate over array from the end
while (l--) {
  // check value present in arrOne
  if (arrOne.indexOf(arrTwo[l]) > -1)
  // if present then remove and insert it
  // at the beginning of arrThree
    arrThree.unshift(arrTwo.splice(l, 1)[0])
}

console.log(arrTwo, arrThree);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Hi You can use filter function to filter array. try with below code.

    var arrOne = [1, 4, 7];
    var arrTwo = [2, 3, 5, 1];
    var arrThree = [];

    function checkValue(a) {
        return !arrOne.indexOf(a);
    }

    function checkValue2(a) {
        return arrThree.indexOf(a);
    }
    function myFunction() {
        arrThree = arrTwo.filter(checkValue);
        document.getElementById("demo").innerHTML = arrThree ;
        arrTwo = arrTwo.filter(checkValue2);
        document.getElementById("demo1").innerHTML = arrTwo;

    }
Veer
  • 6,046
  • 1
  • 14
  • 12
0

var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];


var arrThree = diff(arrOne,arrTwo);//passes the two arrays
console.log(arrThree);


function diff(one, two){
  one.forEach(function(e1){ //iterate through the first array
  
    two.forEach(function(e2){//iterate through second
     
      if(e1 == e2) //checking if elements are equal
       two.pop(e2);//removing from second array
   
    });
 });
  return two; //returning new array
}

You can use underscore js for easy array operations, the difference operation will be _.difference([1, 2, 3, 4, 5], [5, 2, 10]);.

0
   var array1 = [1, 2, 3, 4, 5, 6],
   var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

  var common = $.grep(array1, function(element) {
   return $.inArray(element, array2) !== -1;
  });

  console.log(common); // returns [1, 2, 3, 4, 5, 6];        

  array2 = array2.filter(function(obj) {
   return array1.indexOf(obj) == -1;
  });

  // returns [7,8,9];
0

Since the arrays are sorted you could read them in parallel : O(n) instead of O(n2). Don't use a library for such a simple problem, this is overkill :-)

var i = 0, j = 0;
var a = [1, 4, 7];
var b = [1, 2, 3, 4, 5];
var c = [];

while (i < a.length && j < b.length) {
  if (a[i] < b[j]) i++;
  else if (a[i] > b[j]) j++;
  else c.push(b.splice(j, 1)[0]);
}

console.log("a " + toString(a));
console.log("b " + toString(b));
console.log("c " + toString(c));

function toString (v) {
  return "[ " + v.join(" ") + " ]";
}

Trace :

#0 init

  a = [ 1 4 7 ]
        i
  b = [ 1 2 3 4 5 ]
        j
  c = []

#1 a[i] = b[j] => move b[j] to c

  a = [ 1 4 7 ]
        i
  b = [ 2 3 4 5 ]
        j
  c = [ 1 ]

#2 a[i] < b[j] => increment i

  a = [ 1 4 7 ]
          i
  b = [ 2 3 4 5 ]
        j
  c = [ 1 ]

#3 a[i] > b[j] => increment j

  a = [ 1 4 7 ]
          i
  b = [ 2 3 4 5 ]
          j
  c = [ 1 ]

#4 a[i] > b[j] => increment j

  a = [ 1 4 7 ]
          i
  b = [ 2 3 4 5 ]
            j
  c = [ 1 ]

#5 a[i] = b[j] => move b[j] to c

  a = [ 1 4 7 ]
          i
  b = [ 2 3 5 ]
            j
  c = [ 1 4 ]

#6 a[i] < b[j] => increment i

  a = [ 1 4 7 ]
            i
  b = [ 2 3 5 ]
            j
  c = [ 1 4 ]

#7 a[i] > b[j] => increment j

  a = [ 1 4 7 ]
            i
  b = [ 2 3 5 ]
              j
  c = [ 1 4 ]

#8 j = length of b => done