Skip to content

Instantly share code, notes, and snippets.

@alejandrolechuga
Created August 21, 2012 00:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alejandrolechuga/3409741 to your computer and use it in GitHub Desktop.
What's the best way to compare two Javascript arrays and create a new array of only the matches?
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment