Created
August 2, 2015 20:46
-
-
Save alexandrebvd/065836f527a59527938a to your computer and use it in GitHub Desktop.
20.Bonfire: 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) { | |
function getDiff(arr1, arr2) { | |
var newArr = []; | |
for (var i = 0; i < arr2.length; i++) { | |
if (arr1.indexOf(arr2[i]) === -1) { | |
newArr.push(arr2[i]); | |
} | |
} | |
return newArr; | |
} | |
var newArr = []; | |
newArr.push(getDiff(arr1, arr2)); | |
newArr.push(getDiff(arr2, arr1)); | |
console.log(newArr); | |
var flat = newArr.reduce(function(a, b) { | |
return a.concat(b); | |
}); | |
return flat; | |
} | |
diff([1, 2, 3, 500], [1, 2, 3, 4, 5]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment