-2
var rundhalsArray = ["50237451_001", "50237451_100"];
var Array = ["50237451_001", "50237451_100", "50236765_001", "50236765_100"];

I have two arrays and would like to only display the items that match when comparing them so the result of the above would be:

var resultArray = ["50237451_001", "50237451_100"];
Andy
  • 29,707
  • 9
  • 41
  • 58
user1937021
  • 10,151
  • 22
  • 81
  • 143

2 Answers2

2

http://jsfiddle.net/kL69J/

This is how I would do it:

var array1 = ["a", "b", "c", "d", "e", "f"];
var array2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
var foo = [];

$.grep(array2, function(el) {

    if ($.inArray(el, array1) != -1) {
        foo.push(el);
    }

});


alert(" they have the same " + foo);
Pedro Estrada
  • 2,384
  • 2
  • 16
  • 24
1

JSFIDDLE DEMO

var rundhalsArray = ["50237451_001", "50237451_100"];
var arr = ["50237451_001", "50237451_100", "50236765_001", "50236765_100"];

var result = [];
$.map(rundhalsArray, function (val, i) {
    if ($.inArray(val, arr) > -1) {
        result.push(val);
    }
});
console.log(result);
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56