Created
April 26, 2014 00:10
-
-
Save thecneu/11307661 to your computer and use it in GitHub Desktop.
Intersection and contains - stripped from underscore
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
function contains(a, obj) { | |
var i = a.length; | |
while (i--) { | |
if (a[i] === obj) { | |
return true; | |
} | |
} | |
return false; | |
} |
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
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