Created
February 27, 2012 19:25
-
-
Save julian-amaya/1926456 to your computer and use it in GitHub Desktop.
Array intersect in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.intersect = | |
function() { | |
if (!arguments.length) | |
return []; | |
var a1 = this; | |
var a = null; | |
var res = [] | |
var n = 0; | |
var b = {}; | |
for(var i =0; i<a1.length;i++){ | |
b[a1[i]] = a1[i]; | |
} | |
var n = 0; | |
while(n < arguments.length) { | |
a = arguments[n]; | |
for(i =0; i<a.length;i++){ | |
if (b[a[i]]){ | |
res.push(a[i]); | |
} | |
} | |
n++; | |
} | |
return res; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment