Created
October 6, 2015 17:33
-
-
Save ArielLeslie/e03e04580d48712442dd to your computer and use it in GitHub Desktop.
Diff Two Arrays
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
function diff(arr1, arr2) { | |
//just add two two arrays together | |
var newArr = arr1.concat(arr2); | |
//then filter it | |
return newArr.filter(function(val){ | |
// return true if the value is not in one of the arrays | |
if (arr1.indexOf(val) < 0 || arr2.indexOf(val) < 0){ | |
return true; | |
}else{ | |
return false; | |
} | |
}); | |
} | |
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment