-7

Having some problems here with arrays. I basically want to loop through array1, check which elements in that array exist in array2 and then return a new array containing all matches.

How would one do that?

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
user3122094
  • 241
  • 1
  • 2
  • 15
  • Take a look at https://code.google.com/p/javascriptsets/ and study the intersection function. It should give you a place to start. – Cameron Tinker Jun 26 '14 at 15:12
  • 1
    What problems are you having? Show us some code. – putvande Jun 26 '14 at 15:12
  • the main problem is that i dont have any code and have no idea how to even begin coding it. – user3122094 Jun 26 '14 at 15:13
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide ... there you find general information about JS and arrays. – Felix Kling Jun 26 '14 at 15:17
  • 2
    This question does not provide a minimum understanding of the problem. – Cameron Tinker Jun 26 '14 at 15:17
  • answer is var intersection = array1.filter(function (el) {return array2.indexOf(el)>=0 ; } ); go watch mdn and now delete your question. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Properties) – GameAlchemist Jun 26 '14 at 15:18
  • Don't forget to downvote both this very poor question and the ones answering it to get easy points. – GameAlchemist Jun 26 '14 at 15:27
  • possible duplicate of [Simplest code for array intersection in javascript](http://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – Brigand Jun 26 '14 at 15:31

3 Answers3

0

This code is in pure javascript not with library

var fruits1 = ["Banana", "Pear"];
var fruits2 = ["Banana", "Orange", "Apple", "Mango"];
var fruits3 = [];

for (i=0; i< fruits1.length; i++)
   if (fruits2.indexOf(fruits1[i]) > -1)
     fruits3.push(fruits1[i]);
DavideDM
  • 1,475
  • 16
  • 18
-2

If you have unsorted arrays with different lengths, this will work too.

Create a function called intersect for example:

function intersect(arr1,arr2){
    //We need to know which array is the shortest to avoid useless loops
    if(arr2.length<arr1.length){
        var temp = arr1;
        arr1 = arr2;
        arr2 = temp;
    }
    // Now, we are sure arr1 is the shortest
    var result = [];
    for(var i = 0; i<arr1.length; i++){
        if(arr2.indexOf(arr1[i])>-1) result.push(arr1[i]);
    }
    return result;
}

This function takes 2 arrays as parameters, and no need to worry about their order.

Then you can call it and see the results:

var arr1 = [1,2,3,4,5];
var arr2 = [4,5,6,7,8,9,10,12];

var inter = intersect(arr1,arr2);
alert(inter);

JS Fiddle Demo

blex
  • 24,941
  • 5
  • 39
  • 72
  • @user3122094 You're welcome! However I just noticed I used a wrong sign to make sure we do less loops, it should be `if(arr2.length – blex Jun 26 '14 at 16:43
-3

Vanilla Javascript

for(var x=0;x<array1.length;x++){
    for(var y=0;y<array2.length;y++){
        if ( array1[x] == array2[y] ) array3.push(array1[x]);
    }
}

Jquery version

$.each(array1, function(k,v){
    $.each(array2, function(k2,v2){
       if ( v == v2 ) array3.push(v2);
    });
});