Created
April 26, 2012 18:58
-
-
Save skyl/2501981 to your computer and use it in GitHub Desktop.
Javascript array, is Sub Set
This file contains 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
isSubSet = function(ar1, ar2) { | |
// Test if ar1 is a subset of ar2 | |
ar1 = _.uniq(ar1); | |
ar2 = _.uniq(ar2); | |
if (ar1.length > ar2.length) { | |
return false; | |
} | |
_.each(ar1, function(el){ | |
if (ar2.indexOf(el) === -1){ | |
return false; | |
} | |
}); | |
return true; | |
} | |
ar1 = [1,2,3]; | |
ar2 = [1,2]; | |
ar3 = [1, 2, "foo"]; | |
b = ["a", "b", "c"]; | |
ar4 = [2, b]; | |
ar5 = [b]; | |
//alert(isSubSet(ar5, ar4)); | |
//alert(isSubSet(ar5, ar5)); | |
//alert(isSubSet(ar4, ar5)); | |
//res = isSubSet(ar1, ar2); | |
//alert(res); | |
//res = isSubSet(ar2, ar1); | |
//alert(res); | |
//res = isSubSet(ar3, ar2); | |
//alert(res); | |
//res = isSubSet(ar2, ar3); | |
//alert(res); | |
z = [["foo", "bar"], b]; | |
x = [["foo", "bar"], ["a", "b", "c"], 5]; | |
//alert(isSubSet(z, x)); | |
// BAD CASES ?? | |
//alert(isSubSet([], [1,2,3])); //true - I think this is right. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/SBwpn/3/