public
Last active

What's the best way to compare two Javascript arrays and create a new array of only the matches?

  • Download Gist
maybe.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var a = ["a","m"],
b = ["a","c","x","l","m"] ,
c = [], //array with matches
i = a.length,
j;
 
while(i--) {
j = b.length;
while (j--) {
if (b[j] == a[i]) {
c.push(a[i]);
}
}
}
console.log(c);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.