Skip to content

Instantly share code, notes, and snippets.

@thecneu
Created April 26, 2014 00:10
Show Gist options
  • Save thecneu/11307661 to your computer and use it in GitHub Desktop.
Save thecneu/11307661 to your computer and use it in GitHub Desktop.
Intersection and contains - stripped from underscore
function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
function intersection(list) {
if( list == null ) return [];
var result = [], set = list[0];
var setsLength = list.length;
for( var i = 0, length = set.length; i < length; i++ ) {
var item = set[i];
if( contains(result, item) ) continue;
for( var j = 1; j < setsLength; j++ ) {
if( !contains(list[j], item) ) break;
}
if( j === setsLength ) result.push( item );
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment